- Why the compiler gives me an error when I don't initialize a value to a static member variable? Shouldn't it be initialized to 0?
- Why I have to initialize member variable outside the class? (is this illegal because if you do so, and change the value of this static member variable inside the main function and you create an object of this clas, it will re-assign the static mamber variable to the old value) whereas const static member variable are legal to be initilized inside the class (and this is possible because you can't change the value of this static member variable anyway)?
Error: undefined reference to class_name::a
extern
in a header but not actually defining it in a .cpp file. A global variable must have a consistent memory location known to all compilation units, otherwise they won't see each other's changes to its value. As such, it must have external linkage and be defined exactly once. It is not like astatic
(compilation unit local) variable. – pmdj