Static members of a class can be divided into static data members and static function members. Let's take a look at the static data members of the class: 1. What is the use of static data members of the class?
Classes encapsulate data and data operations. Each Class Object is allocated with memory to save its own data members. However, many objects of a class need to share data, for example, the Person class is defined and the person1, person2, and person3 are instantiated. But now I want to know how many person objects have been instantiated. What should I do? One idea is to define a count data, which is shared by these person objects. Each person can access the count data member. This is the role of the static data member of the class: static members are used to share data and functions between different objects in the same class.
2. How to define a static data member using the keyword static, such as static int count. iii. Example code: <span style = "font-size: 14px;"> class Point {public: Point (int m = 0, int n = 0)
{This-> x = m; this-> y = n; this-> count ++ ;}~ Point () {this-> count --;} int getX () {return this-> x;} int getY () {return this-> y;} void ShowCount () {cout <"The numbers of count are:" <this-> count <endl;} private: int x; int y; static int count ;} <br> int Point: count = 0; </span> count is defined as a static member variable, so count can be shared by all Point objects through the object name. ShowCount to access count: Point A (4, 5);. showCount (); // 1 Point B (A); B. showCount () // 2 count is a static member. The creation and destruction of each object will affect count. 4. Notes for static data members 1. static data members do not belong to any object. They only belong to Classes.
2. the initialization of static data members should be performed before the object is instantiated. Otherwise, you should use the Class Name: static data members to access the object (this can be done without instantiating the object) and output the error result.
3. Generally, static members cannot be accessed in inline functions.
5. Static function members of the class. In the preceding example, each object is copied to a ShowCount object, which can be set as a shared mode like static data members of the class. This must be defined as a static function member.
Related definitions: class Point {public: Point (int m, int n) {x = m; y = n; count ++;} Point (Point & p );~ Point () {count --;} static void ShowCount () {cout <count <endl;} private: int x; int y; static int count;} int Point :: count = 0; // note the initialization method in the following code: Point A ();. showCount (); // use the object name to reference Point: ShowCount () // use the class name. We recommend that you use this method, this pointer cannot be used in static member functions. It is generally used to access static data members and is not used for other purposes. (To access non-static members, you must pass the object name through the parameter and access it through the object name ).