class TConst
{
const int i;
int& ref;
public:
TConst(int n):i(n),ref(n){}
static void p1(){prn(i);}//error here
};
My compiler generates an error when I try to use a const class member in a static member-function.
Why is it not allowed?
The
constmember is initialized during the object construction. Thestaticmembers are not dependent on the object creation and don’t have access tothispointer hence they don’t know where yourconstmember variable resides.