Static member of the class
Both the member variables and member functions of the class can be declared as static.
1. static members of a class belong to the class, so the instance objects of this class are shared but not part of an object. No matter whether or not an instance of many classes is created or not, the static member exists and only one copy exists.
2. static members are also subject to access permissions.
3. You can use the scope operator to directly access static members from the class, or indirectly access static members through objects, references, or pointers to the class type.
4. The static member function does not have a static parameter. Therefore, the non-static member of the callback class cannot be directly used. You can directly use the static member of the callback class.
Description and definition of static members
Static member variables are declared in the class and are defined outside the class. Static member functions can both declare and implement in the class, or put the implement out of the class.
1. To declare a static member, you only need to add the static keyword at the beginning of the member declaration. (Declare and add static)
2. Neither static member variables nor static keywords can be added to definitions and initialization outside the class or member functions outside the class. (Static is not defined.) otherwise, the compiler reports error: cannot delare member function to have static linkage.
Static member variable
Static member variables can be used to count and control the number of instances of the class. It can be used to implement the singleton mode.
1. static member variables must be defined once (and only once) outside the class definition ).
2. The static member cannot be initialized through the constructor (because the static member variable does not belong to a certain object), but should be initialized during definition.
The best way to ensure that objects are exactly defined once is to put the definition of static data members in a non-inline member function definition file containing classes.
Double Account: interestRate = initRate ();
Here initRate () is a static member function. Although initRate () is private, we can still use it to initialize initerestRate.
3. Generally, the type of a non-static member cannot be the class type of the member. Otherwise, it is automatically recursive during initialization. The static member variable type can be the class type of the member. (It is a static member and has only one copy. It will not be wirelessly recursive)
Special integer const static member
As mentioned above, static member variables of a class are the same as normal member variables and cannot be initialized in the class definition body. Class definitions must be initialized in vitro. This is an exception when the integer const static member variable is used.
1. As long as the initialization is a constant expression, the integer const static member variable can be initialized in the class definition body.
2. A constant expression used to initialize the integer const static variable with a constant value. It can be used in any place where constant expressions are needed.
3. After the const static member variable is initialized in the class definition,Still requiredThe class definition is defined in vitro once (no need to specify the initial value ).
Static member functions
1. The static keyword must be specified for the declaration of the static member function in the class body, but the definition in the class body does not need to be repeated.
2. Keep in mind the rules for using static member functions: a static member function does not have the this parameter. It belongs to the entire class, rather than the component of an object.
2.1 therefore, the static member function cannot directly access non-static members. (Non-static members will display or implicitly use this pointer)
The 2.2static member is not part of any object, so the static member function cannot be declared as Const. Because declaring a member function as const promises not to modify the object to which the function belongs. The static member function does not even have the corresponding object.
2.3static member functions cannot be declared as virtual functions. (Wait until the part of the virtual function is explained)
C ++ uses static members to implement Singleton pattern)
Steps:
1. Set the default constructor permission of the class to private;
2. Use a private static member variable to save the pointer to this unique instance;
3. Use a private static member function to initialize the instance pointer in step 2 (equivalent to a private constructor)
4. Provide a public static function to obtain the pointer to a unique instance;
Example:
Class Sing {private: Sing (); // The constructor sets private static Sing * Instance (void); static Sing * psing; public: static Sing * GetSingInstance (void );}; sing * Sing: psing = Instance (); Sing * Sing: Instance (void) {return new Sing;} Sing: Sing () {cout <"Sing constructor invoked \ n";} Sing * Sing: GetSingInstance (void) {return psing ;}