For all objects of a specific class type, we need to access a common variable. In this case, we usually declare a static variable in the class Declaration, that is, the static variable. Of course, we can use a global variable instead, but it is a bad habit to use global variables. The important thing is that global variables will destroy the class encapsulation. However, static variables in a class do not exist as common variables. They exist independently with any objects of the class. Each static data member is connected to a specific class, not to objects of the class.
I. advantages of using static members of a class:
1. The static member name is in the scope of the class, so it can avoid conflicts with the names of other class members or global objects.
2. encapsulation can be implemented. Static members can be private members, but global objects cannot.
3. The reading program shows that static members are associated with specific classes. This visibility clearly shows the programmer's intent.
2. static class member life and definition http://www.bkjia.com
Static class members are declared when they declare the class. The definition of static class members should be placed in the implementation file of the class. That is, the class declaration is in A. h, while the implementation of the class is in A. cpp, and the definition of the static type variable is in A. cpp.
Format: <data type> <class name >:< static variable name >=< initial value>
3. Modify the variable of the const type in static mode.
When static modifies a variable of the const type, the variable can be directly initialized in the class declaration, but the data member must still be defined outside the definition body.
Author Ling Feng