Alex Rivera | Logout

What if the default parameter value is defined in code not visible at the call site?

Asked 2009-07-17T09:16:13.257
9

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
#include <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?

Edit
Report

1 Answer

0

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

class ClassA {
public:
    void Enable( bool enable = true );
};
answered 2009-07-17T09:19:05.387

Your Answer