tags:

views:

145

answers:

4

I've found some strange code...

//in file ClassA.h:
class ClassA {
public:
    void Enable( bool enable );
};

//in file ClassA.cpp
#include <ClassA.h>
void ClassA::Enable( bool enable = true )
{
   //implementation is irrelevant
}

//in Consumer.cpp
#inclide <ClassA.h>
....
ClassA classA;
classA.Enable( true );

Obviously since Consumer.cpp only included ClassA.h and not ClassA.cpp the compiler will not be able to see that the parameter has a default value.

When would the declared default value of ClassA::Enable in the signature of the method implementation have any effect? Would this only happen when the method is called from within files that include the ClassA.cpp?

A: 

Put the default value in the declaration, not the definition.

class ClassA {
public:
    void Enable( bool enable = true );
};
280Z28
Who marked this down?? If you mark me down, tell me why so I can at least fix it.
280Z28
Actually, it was me who marked this down and the reason was that the OP obviously knows that the correct way is to put this in the declaration not the definition. The OP was wondering that what are the effects of putting such a thing in definition and you answered without reading the question thoroughly. And yes, sorry for not letting you know the reason of marking it down. I should have done that.
Aamir
The OP made no mention of putting it in the declaration, so I suggested a solution to the visibility problem which he did mention?
280Z28
OP didn't say or imply he had a visibility problem. He asked the effect of a particular piece of code. IMO this is a good answer if someone finds this page searching for information on how to specify default parameters, but it's not an answer to the question and not likely to be of use to the OP. Whether it therefore deserves an upvote or a downvote is beyond me. +0 :-)
Steve Jessop
+3  A: 

Default values are just a compile time thing. There's no such thing as default value in compiled code (no metadata or things like that). It's basically a compiler replacement for "if you don't write anything, I'll specify that for you." So, if the compiler can't see the default value, it assumes there's not one.

Demo:

// test.h
class Test { public: int testing(int input); };

// main.cpp
#include <iostream>
// removing the default value here will cause an error in the call in `main`:
class Test { public: int testing(int input = 42); };
int f();
int main() {
   Test t;
   std::cout << t.testing()  // 42
             << " " << f()   // 1000
             << std::endl;
   return 0;
}

// test.cpp
#include "test.h"
int Test::testing(int input = 1000) { return input; }
int f() { Test t; return t.testing(); }

Test:

g++ main.cpp test.cpp
./a.out
Mehrdad Afshari
A: 

Would this only happen when the method is called from within files that include the ClassA.cpp?

That's correct. But note that doing so will almost certainly produce multiple definition errors, so the default is only really available from its point of definition within ClassA.cpp.

anon
It's not an error. It'll produce errors only if both definitions provide default values.
Mehrdad Afshari
The actual functions will cause multiple definition errors if the file is #included multiple times, which is what I was referring to.
anon
+1  A: 

Let me admit first that this is the first time I have seen this type of code. Putting a default value in header file IS the normal practice but this isn't.

My guess is that this default value can only be used from code written in the same file and this way the programmer who wrote this wanted to put it in some type of easiness in calling the function but he didn't want to disturb the interface (the header file) visible to the outside world.

Aamir