C++ Object ModelC++ Object Model

C++ Object ModelC++ Object Model

C language data and functions

In C language, data and functions are declared separately.

  • data
typedef struct point2d
{
    float x;
    float y;
} Point2d;
  • function

Print the value of Point2d

void Point2d_print(const Point2d * pd)
{
    printf("(%f, %f)", pd->x, pd->y);
}

C++ class

Also realize the function of C language, the following definition can be made in C++

class Point2d
{
    Point2d(float x, float y):m_x(x), m_y(y){}
    
    print()
    {
        printf("(%f, %f)", m_x, m_y);
    }
    
private:
    float m_x;
    float m_y;
};

Question: Point2d from C to C++, what changes have there been in the memory layout?

The answer is no change.
In this example, the data members in C++ are the same as those in C. The difference is that there is an additional non-static member function print in C++, but the non-static member function and data members are separated and do not occupy the size of the class. That is to say, the call of the print function is actually print(Point2d* pd), just like the call of Point2d_print in C language.

The additional burden of C++ memory layout is caused by virtual, which mainly includes virtual functions and virtual base classes.

C++ object model

In C++,
there are two types of class data members: static (static) and non-static (nonstatic)
There are three types of class member functions: static functions (static), non-static functions (nonstatic) and virtual functions (virtual)

C++ object model:

  1. Nonstatic data members are inside the class
  2. static outside the class
  3. Static functions (static) and non-static functions (nonstatic) outside the class
  4. Virtual function (virtual):
    a. Each class generates a bunch of pointers to virtual functions and puts them in the virtual table (virtual table, vtbl)
    b. Adds a pointer to the virtual table (vptr) for each class object.
    c. The type_info (used to support RTTI) of each class is also specified by the virtual table, which is generally located at the first slot.
class Point2d
{
    Point2d(float x, float y):m_x(x), m_y(y),m_pointCount(2){}
    virtual ~Point2d(){}
    
    static int getPointCount(){return m_pointCount;}
    
    print()
    {
        printf("(%f, %f)", m_x, m_y);
    }
    
private:
    float m_x;
    float m_y;
    static int m_pointCount;
};

The C++ object model of the above code is as follows

+---------------+
|  float m_x;   |            virtual table
+---------------+
|  float m_y;   |          +---------------+
+---------------+          |               |      +--------------------+
|  vptr Point2d +---------->               +------> type_info(RTTI     |
+---------------+          +---------------+      +--------------------+
                           |               |      +--------------------+
   Point2d pt2;            |               +------> Point2d::~Point2d()|
                           +---------------+      +--------------------+


+-------------------------------------+       +------------------------------------+
| static int Point2d::m_pointCount    |       | Point2d::Point2d(float x, float y) |
+-------------------------------------+       +------------------------------------+

+-------------------------------------+       +------------------------------------+
| static int Point2d::getPointCount() |       | Point2d::print()                   |
+-------------------------------------+       +------------------------------------+


Reference:

  1. Explore the C++ object model in depth

Guess you like

Origin blog.csdn.net/itas109/article/details/132031941