Possible Duplicate:
Nonstatic member as a default argument of a nonstatic member function
Why can member variables not be used as defaults for parameters?
Ok I apologize in advance if I am not seeing something very simplistic here or forgetting some fundamental rule of C++ but I am not sure why this does not work as expected.
Here is a example of code that I can’t get to work
class Foo
{
private:
Bar *ptrBar;
public:
void doSomething(int x, Bar *p = ptrBar);
}
The compiler is having an issue with the default parameter to this function. Is there some reason why this wouldn’t work.
Basically doSomething would do some operation on a Bar object, and I want it to be the one pointed to by ptrBar by default. Everything seems sound unless I am forgetting something?
At the time the compiler is evaluating this,
ptrBaris not set. Therefore your default parameter has no default value.Why not use a default of null and if
pindoSomething()is null then use theptrBarmember.