A schematic of my problem…
class A
{
public:
// etc.
protected:
uint num;
};
class B : public A
{
public:
void foo(uint x = num); //bad
};
gives this error:
error: invalid use of non-static data member ‘A::num’
error: from this location
Why does this happen, and what can I do to work around this?
I suspect this happens (based on the complaint about non-staticness) because there is no
thispointer for it to use to know which instance of B it should getnumfrom.The Microsoft compiler (at least) allows you to specify an expression, but not a non-static member. From MSDN:
Work-arounds for this are numerous and others have pointed out a few. Here’s one more which you may or may not like: