Static and non-static members of C++ classes

Static members of the class do not occupy the number of bytes of the class object

1. The number of bytes occupied by an empty class is 1;
2. The number of bytes occupied by the int integer variable is 4;

class Person
{
    
    
	int m_A;  //非静态成员变量,属于类的对象上

	static int m_B; //静态成员变量,不属于类的对象上

	void func(){
    
    }  // 非静态成员函数,不属于类的对象上

	static void func1(){
    
    } //静态成员函数,不属于类的对象上
};

3. Test code

    Person p1;
	cout << "size of p1=" <<sizeof(p1)<< endl;

4. Test results

size of p1=4
请按任意键继续. . .

In summary, the conclusions are as follows:
static member variables and functions, on objects that do not belong to the class;
non-static member variables, on objects that belong to the class;
non-static member functions, on objects that do not belong to the class;

Guess you like

Origin blog.csdn.net/Little_XWB/article/details/108184988