• Static class members: they are member data and member functions related to the class, rather than Member Data and member functions related to the class objects.
Static member data is also called class data, and static member functions are also called class methods. Static member data is only a description in the class and also needs a definition (or initialization ). Static member data must be initialized out of the class definition (with the class name limitation), and the program can only be provided once, so initialization cannot be placed in the header file.
Example 1:
Class test {public: static int K; test (int A): K (a) {// compilation Error !!!}};
//error: 'int Test::k' is a static data member; it can only be initialized at its definition
Example 2:
C ++ requires that the const static class members can be directly initialized. Other non-const static class members need to be initialized outside the class declaration. We generally choose to initialize in the class implementation file.
int Test::k;
The default value is 0;
You can also specify the following options:
int Test::k(20);
class Test{public:static const int a = 10; static int k;};int Test::k;int main(){cout << Test::k << endl;cout << Test::a;return 0;}
• Do not use static member data in the inline function, because the compiler cannot guarantee that the static member data has been initialized at this time.
class Test{public:static const int a = 10;static int k;void f(){k++;}};int Test::k(20);int main(){Test t;t.f();cout << Test::k;return 0;}
class A{public:A(A & e):_e3(e){}A & _e3;A * _e1;static A _e; A _e2; // error C2460: '_e2' : uses 'A', which is being defined };
• Comparison of static member data with global variables: static member data has only one copy regardless of the number of instances in the class, which is similar to global variables. However, static member data has a scope name and is not necessarily public.
• Static member functions cannot be declared as const and volatile.
• Comparison between static member functions and friend functions: both static member functions and friend functions do not have an implicit this pointer, and both can be used in the private and protected sections of the member class. However, a static member function has a scope name and is not necessarily public.
• Const static member data: in some C ++ compilers, ordered (such as int, unsignedlong, and char) const static member data can be
Initialize it in the class.
• Static member data initialization sequence: The data sequence of static initialization members is related to the lifecycle of static objects in the class scope, file scope, and objects in the namespace scope. When the static initialization of different compilation units (that is, the. cpp file) has sequential dependencies, this may be risky. The solution is to convert static member data to static member functions.
• Volatile: when the value of an object may be changed beyond the control or monitoring of the compiler, the object should be declared as volatile.
In this case, some routine optimizations executed by the compiler cannot be applied to it. Volatile can also modify class member functions. For a volatile class object, it can only call volatile member functions, constructors, and destructor.