The meaning of static in class in C ++

Static decorated members of the class, said the class's shared data

static decoration class members

As mentioned in C ++ primer, static class members are not like ordinary class data members. Static class data members are independent of all class objects. Static class data members are associated with the class, but do not have any relationship with the objects defined by the class. That is, static does not have a copy of each class object like ordinary class data members. All class objects share a static class member.
For example, the static member of class A object is changed 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

class Text  
{  
    public:  
    static int count;  
};  
  
int Text::count=0;//用static成员变量必须要初始化   
  
int main()  
{  
    Text t1;  
    cout<<t1.count<<endl;   
    return 0;  
}//程序输出0

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

class Text  
{  
    public:  
    static int count;  
};  
  
int Text::count=0;//用static成员变量必须要初始化   
  
int main()  
{  
    Text t1;  
    Text t2;  
      
    t1.count = 100;     //t1对象把static成员count改为100   
    cout<<t2.count<<endl; //当t2对象打印static成员的时候,显示的是100而不是0   
    return 0;  
}

benefit:

The member variable modified with static does not occupy memory in the object, because it is not generated on the heap or stack with the object, and the variable modified with static is generated in the static storage area. So the advantage of using static modification on the one hand is to save the memory space of the object. Just as you create 100 Person objects, and all 100 objects have a common variable, for example, the nationality variable, that is, the nationality of the Person object is the same.
If the nationality variable is modified with static, even if there are 100 Person objects, 100 nationality variables will not be created, only one statically modified nationality variable is needed. When these 100 objects are to be used, statically modified nationality variables will be called. Otherwise, if there are 100 Person variables, 100 nationality variables will be created. If the nationality variables are the same, it is a waste of space.

static modified class member function

Because statically modified class members belong to the class, not to the object, the static class member function does not have this pointer, this pointer is a pointer to this object. Because there is no this pointer, static class member functions cannot access non-static class members, only static-modified class members.

class Text  
{  
    public:  
    static int fun()  
    {  
        return num;  
    }  
    static int count;  
    int num;  
};  
int Text::count=5;//用static成员变量必须要初始化   
  
int main()  
{  
    Text t1;  
    Text t2;  
    t1.num=100;  
      
    t1.fun();//发生错误,fun函数return的是非static类成员 如果return count就正确   
    return 0;  
}
Published 44 original articles · praised 8 · visits 6734

Guess you like

Origin blog.csdn.net/zhaodeming000/article/details/105433546