Private static class members
c++
Solution
Yes, it does mean something. Consider the following example, which throws a compiler error, because the member is `private`. Being able to initialize a private variable is not the same as being able to change it from any context.
class Something
{
private:
static int s_nValue;
};
int Something::s_nValue = 1;
int main(){
Something::s_nValue = 2; // Compiler error here.
}
Problem
When we declare a member variable static, it is shared between all instances of the class. I've heard that you should think of the variable belonging to the class itself, not any instance. This lets us initialize the variable without instantiating any object of the class, which makes sense. ``` class Something { public: static int s_nValue; }; int Something::s_nValue = 1; ``` But why are we allowed to initialize a private static member? ``` class Something { private: static int s_nValue; }; int Something::s_nValue = 1; ``` Does private even mean anything when we are talking about static members?