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?
const means different things. In this case, it means that i is immutable after it's been initialized. It doesn't mean it's a literal constant (like I believe you think it means). i can be different for different instances of TConst, so it's logical that static methods cannot use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With