We declare a static
variable in a class and initialize the variable outside the class, but we use the variable within the function.
Anyone tell me the reason why? Thanks in Advance
I'm not sure but my guess is, because inside a class member variables are only declared. They are initialized through a constructor or other member functions.
This happens when an object is instantiated. However for static members, the objects need not be instantiated. Hence they need to be initialized once outside of the class.
EDIT:
Actually it is not necessary to initialize the static variables, but it is necessary to define them outside the class to allocate memory for them. Only after their definition, can they be initialized and then used in the program.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments