Static
For all objects of a specific class type, it is sometimes necessary to access a global object. However, for a class, global objects may destroy encapsulation. Instead, the class defines static members to replace a universally accessible global object.
Static members include static data members and static member functions.
Generally, non-static data members exist in each object of the class type, while static data members exist independently of all objects of the class. Each static data member is associated with the class, instead of associating with class objects.
Advantages of replacing global objects with static members:
The function name of the static member is in the scope of the class, so it can avoid conflicts with other members or global object names.
Encapsulation can be implemented, that is, static members can be private members, but global objects cannot.
Template <class T>
Class
{
Public:
Static t test ();
PRIVATE:
Static t var;
}
There is a separate var object in this class and the test () member function is owned by all objects in Class.
You can call static members directly from a class through the scope operator or indirectly through objects, references, and pointers to objects of this type.
Static member functions
The static function does not have the this pointer.
Because static members are components of a class, rather than components of any object, the static member function does not have the this pointer.
Static member functions cannot be declared as const functions.
Because declaring a member function as a const promises that the object to which the function belongs will not be modified (because the static function is not an integral part of any object, why bother with this? A little open, huh ).
Static member functions cannot be declared as virtual functions.
Because the static member function belongs to the class without the this pointer, and the virtual function must have the this pointer's sentiment, it determines how to call (polymorphism) according to the class actually pointed to by this pointer ).
About Staic data members
After a static data member is declared in the class definition body, it must be defined outside the class definition body and exactly once. To ensure that it is defined exactly once, the general practice is to put it in a file that contains the class's non-inline member function definition. Similarly, static keywords cannot be repeatedly specified in the external definition of the class definition body;
Static data members cannot be initialized through class constructor. Generally (except for the integer const static variable), they are initialized when the class definition body is defined externally;
An integer const static data member can be specified for initialization within the class definition body (provided that the initialization expression is a constant );
The static data member type can be the class type of the member. Non-statiic members are limited to pointers or references declared as their own class objects.
Class
{
Public:
...
PRIVATE:
Static A A1; // OK
A * P1; // OK
A A2; // Error
}
Static data members can be used as default real parameters. Not static members.