tags:

views:

408

answers:

5

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 1st example? Is it some type of practise that states the constructor takes ZERO parameters?

TIA

Andrew

A: 

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.

Annerajb
+12  A: 

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?

dirkgently
At least you're in the positive. I recently had an accepted (correct) answer that ended up with negative votes. Disturbing.
JaredPar
@JaredPar: And I was joking about this too will happen to a colleague a couple of days back. I have 3 accepted answers removed in the last 3 days -- shocking?
dirkgently
@dirkgently, that is odd. I think I average about 1 accepted answer removed every 2-3 weeks. 3 in 3 days is definitely high.
JaredPar
+4  A: 

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... :-)

TofuBeer
A: 

While for all practical purposes as a user of C++, the accepted answer is good enough, it's not entirely correct. Read for example this email thread discussing the difference: http://www.archivum.info/comp.std.c++/2006-04/msg00202.html

That link confirms the accepted answer is correct. It says (void) and () are equivalent in C++, but that in C99 there is a subtle difference - so C++ has a problem, but the accepted answer does not! :) (Also I just checked and it appears that nothing has changed in the current draft of C++0x.)
Daniel Earwicker
Exactly. C++ has a problem with void, which means the accepted answer is precise enough for practical use, but not precise enough for e.g. compiler development. Let's call it incomplete rather than wrong.
yes, the exact answer is alright. i would not call it a "problem" either. like it was in c89, c++ does only accept (void) - i.e it does not semantically check whether in (T), T is void and treat it as () too. see the comment i posted to the question above.
Johannes Schaub - litb
it has so much more meaning in C++ than it has in c99. just look at this trivial sample: template<typename T> void f(T); f<void>(); // valid??
Johannes Schaub - litb
anyway, all answers have to stay incomplete in one or the other form (otherwise, i guess one will have to copy pretty much the whole standard into each answer haha). so i would say it's alright for it to not mention that case, while it would not have hurt to mention it either i think
Johannes Schaub - litb
It's interesting that the C99 definition, if applied to C++, would mean that for any function of a single template parameter, the parameter is optional. But what would that mean in practice? How would you implement the function? I assumed supplying void for T is only valid in return value positions.
Daniel Earwicker
A: 

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.

John Isaacks