Code example from http://www.learncpp.com/cpp-tutorial/812-static-member-functions/:
class Something
{
private:
static int s_nValue;
};
int Something::s_nValue = 1;
This code compiles without warnings or errors. I do not understand why.
Shouldn’t we get a warning for trying to access s_nValue because it is private? Or these access specifiers do not apply to static members?
The definition of
s_nValueis not “accessing” the member from outside the class–it’s actually its implementation. Think of this as being just like the actual implementation of a member function, if placed in the source file outside the declaration for the enclosing class.In other words, access specifiers absolutely apply equally to static members.