Reputation: 10764
Given the following code:
#pragma once
class B
{
public:
B(void)
{
}
~B(void)
{
}
};
I know I can also write this:
#pragma once
class B
{
public:
B()
{
}
~B()
{
}
};
What is the purpose of having void
in the first example? Is it some type of practice that states the constructor take zero parameters?
Upvotes: 17
Views: 14883
Reputation: 61536
A long time ago you did something like this in C (my pre-ISO C is rusty :) ):
void foo(a, b)
int a,
int b
{
}
while C++ was being created the name mangling required the types of the arguments, so for C++ it was changed to:
void foo(int a, int b)
{
}
and this change was brought forward to C.
At this point, I believe to avoid breaking existing C code this:
void foo()
and this:
void foo(void)
meant two very different things, ()
means do not check for the argument number or type, and (void)
means takes no arguments. For C++ ()
meaning not to check anything was not going to work so ()
and (void)
mean the same thing in C++.
So, for C++ ()
and (void)
were always the same thing.
At least that is how I remember it... :-)
Upvotes: 11
Reputation: 57994
I just wanted to add that when you use void after the parameters like function():void it is used to indicate that the function does not return a value.
Upvotes: 0
Reputation: 111200
The two are same, at least in C++. In C, providing an empty pair of parentheses typically means an unspecified parameter list (as opposed to an empty parameter list). C++ does not have this problem.
How can a correct answer get downvoted so many times? Yet another SO bug?
Upvotes: 45
Reputation: 932
afaik if you pass void into the constructor or any function as the argument it means that the function dosnt take any argument so example a and b are equal. but i am not sure if any of them change the function signature in any way or make it run faster etc.
Upvotes: 0