In the process of C ++ object-oriented programming, data between objects is not shared. When designing a class, sometimes data shared between objects is required, in addition to setting the data to be shared as global data or functions, you can also use the static mechanism of c ++.
1. static data member
Class Name {...... Static type description ;...... };
Static data members are shared by all objects, and the memory space occupied by static data members is not allocated because of the generation of an object, nor will they disappear because of the destruction of the object. Like other non-static data members in the class, when it is defined as private, it cannot be accessed by the outside world. However, it can be accessed by any function with the class access permission.
Static data member initialization: A non-static member can be initialized in the constructor, but static cannot be initialized in the constructor. Its initialization can only exist in the global region, to specify the static members of a class, you can use the scope symbol ":" To specify the static members.
Class Name: static data member
That's simple.
When it is declared as public, it can be directly accessed and modifiedDo not use static member functions.
2. static member functions
Class Name {... Static function name (shape parameter) {function body }... };
Static member functions cannot access non-static data members with any permissions. In other words, they can only access static data. It is not easy to call static member functions outside the class.
Object. static member function
This is the practice of common member functions,
Class Name: static member function