C++ static class member, static class member function

Reprint: ZJE_ANDY

The members in the static modification class represent the shared data of the class

1. static class members

C++ primer said that static class members are not like ordinary class data members, static class data members are independent of all class objects. A static class data member is associated with the class, but has nothing to do with the objects defined by that class. That is, static does not have a copy for each class object like ordinary class data members, and all class objects share a static class member. For example, if the class A object modifies the static member to 1, then the value of the static class object member corresponding to the B object will also be 1.

Note: static class objects must be initialized outside the class

1  class Text  
 2  {  
 3      public :  
 4      static  int count;  
 5  };  
 6    
7  int Text::count= 0 ; // The static member variable must be initialized    
8    
9  int main()  
 10  {  
 11      Text t1;  
 12      cout< <t1.count<< endl;   
 13      return  0 ;  
 14 } // Program output 0  

All objects share a static class member

Static modified variables exist before objects, so static modified variables should be initialized outside the class. Because static is something shared by all objects, it must exist before the object.

1  class Text  
 2  {  
 3      public :  
 4      static  int count;  
 5  };  
 6    
7  int Text::count= 0 ; // The static member variable must be initialized    
8    
9  int main()  
 10  {  
 11      Text t1;  
 12      Text t2 ;  
 13        
14      t1.count = 100 ;      // The t1 object changes the static member count to 100    
15      cout<<t2.count<<endl; // When the t2 object prints the static member, it displays 100 instead of 0    
16     return 0;  
17 }  

benefit:

The member variable modified with static does not occupy memory in the object, because it is not generated in the heap or stack together with the object, and the variable modified with static is generated in the static storage area.

Therefore, the advantage of using static modification is that it can save the memory space of the object. As if you create 100 Person objects, and these 100 objects have a common variable, such as the nationality variable, that is, the nationality of the Person object is the same. Then if the nationality variable is modified with static, even if there are 100 Person objects, 100 nationality variables will not be created, and only one statically modified nationality variable is required. When these 100 objects are used, the statically modified nationality variable will be called. Otherwise, if there are 100 Person variables, 100 nationality variables will be created. If the nationality variables are all the same, it will be a waste of space.

2.static class member function

Since the statically modified class members belong to the class, not the object, the static class member function does not have this pointer, and the this pointer is a pointer to the object. Because there is no this pointer, the static class member function

You cannot access non-static class members, only statically modified class members.

1  class Text  
 2  {  
 3      public :  
 4      static  int fun()  
 5      {  
 6          return num;  
 7      }  
 8      static  int count;  
 9      int num;  
 10  };  
 11  int Text::count= 5 ; // must use static member variables To initialize    
12    
13  int main()  
 14  {  
 15      Text t1;  
 16      Text t2;  
 17      t1.num=100 ;  
 18        
19      t1.fun(); // An error occurs, the fun function returns a non-static class member if the return count is correct    
20      return  0 ;  
 21 }  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326076588&siteId=291194637