c ++ object model of

Member functions and member variables stored separately

Only non-static member variables would fall on the object.

Each empty memory space occupied by the object as follows: 1. c ++ compiler will give each empty object is also assigned a memory space is empty in order to distinguish objects occupy memory locations. Each space object should have a unique space.

Function will not take up space objects, all objects share a function instance.

#include<iostream>
using namespace std;
class Person {
public:
    int m_a;
};
class Person1 {
public:
    int m_a;
    static int m_b;
};
void test() {
    Person p;
    Person1 p1;
    cout << sizeof(p) << endl;
    cout << sizeof(p1) << endl;
}
int main() {
    test();
    system("pause");
    return 0;
}

At this output:

1

4

4

Description: the object is also a 1-byte empty space. Static member variables does not belong to a particular object, the same token, static member functions do not belong to a particular object. Furthermore: all objects share an instance of a member function.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12095760.html
Recommended