C++对象数据成员存储示例

本文通过实例探讨了C++对象中数据成员的布局,包括无继承、单一继承、多重继承和虚继承的情况,详细解析了不同继承方式下对象内存的组织方式,特别是虚继承时的数据存储和内存填充规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


本文主要以示例说明C++对象数据成员存储布局,有关理论知识,请参考Data语意学

一、数据成员的布局

1.1、无继承

#include <iostream>
using namespace std;

class X
{
public:
    int a = 0;
    static int staticx;
    int b = 1;
};
int X::staticx = 42;

class Y
{
public:
    int a = 0;
    static int staticy;
public:
    int b = 1;
};
int Y::staticy = 43;

int main()
{
    X x;
    cout << "X:" << endl;
    cout << "sizeof(X) = " << sizeof(X) << endl;
    cout << "vptr address: " << (&x) << endl;
    cout << "x.a address: " << &x.a << endl;
    cout << "x.b address: " << &x.b << endl;
    cout << "x.staticx address: " << &x.staticx << endl;

    Y y;
    cout << "\nY:" << endl;
    cout << "sizeof(Y) = " << sizeof(Y) << endl;
    cout << "vptr address: " << (&y) << endl;
    cout << "y.a address: " << &y.a << endl;
    cout << "y.b address: " << &y.b << endl;
    cout << "y.staticy address: " << &y.staticy << endl;

    return 0;
}

程序运行结果如下:

在这里插入图片描述
从上述结果可以看出:非静态数据成员在类对象中的排列顺序将和其被声明的顺序一样,任何中间介入的静态数据成员都不会被放进对象布局之中。在上述例子中,X和Y对象是由两个int组成的,顺序是a、b。

C++标准要求,在同一个access section(也就是private、public、protected等区段)中,成员的排列只需符合"较晚出现的成员在类对象中有较高的地址"这一条即可。

C++编译器也允许编译器将多个access section之中的数据成员自由排列,不必在乎它们出现在类声明中的顺序。

1.2、单一继承

#include <iostream>
using namespace std;

class X
{
public:
    int ax = 0;
    static int staticx;
    int bx = 1;
    char cx = 'a';
};
int X::staticx = 42;

class Y :public X
{
public:
    int ay = 0;
    static int staticy;
public:
    int by = 1;
};
int Y::staticy = 43;

int main()
{
    X x;
    cout << "X:" << endl;
    cout << "sizeof(X) = " << sizeof(X) << endl;
    cout << "x address: " << (&x) << endl;
    cout << "x.ax address: " << &x.ax << endl;
    cout << "x.bx address: " << &x.bx << endl;
    cout << "x.cx address: " << &x.cx << endl;
    cout << "x.staticx address: " << &x.staticx << endl;

    Y y;
    cout << "\nY:" << endl;
    cout << "sizeof(Y) = " << sizeof(Y) << endl;
    cout << "y address: " << (&y) << endl;
    cout << "y.ax address: " << &y.ax << endl;
    cout << "y.bx address: " << &y.bx << endl;
    cout << "y.cx address: " << &y.cx << endl;
    cout << "y.ay address: " << &y.ay << endl;
    cout << "y.by address: " << &y.by << endl;
    cout << "y.staticy address: " << &y.staticy << endl;

    return 0;
}

程序运行结果如下:
在这里插入图片描述
从上述结果可以看出:在X中,为了对其内存,会填补额外的3个字节。在Y中,首先存放的是X的子对象的数据成员,然后存放Y的数据成员。

X和Y的内存布局如下:
在这里插入图片描述

1.3、多重继承

#include <iostream>
using namespace std;

class X
{
public:
    int ax = 0;
    static int staticx;
    int bx = 1;
    char cx = 'a';
};
int X::staticx = 42;

class Y
{
public:
    int ay = 0;
    static int staticy;
public:
    int by = 1;
};
int Y::staticy = 43;

class M :public X, public Y
{
public:
    int am = 0;
};

int main()
{
    X x;
    cout << "X:" << endl;
    cout << "sizeof(X) = " << sizeof(X) << endl;
    cout << "x address: " << &x << endl;
    cout << "x.ax address: " << &x.ax << endl;
    cout << "x.bx address: " << &x.bx << endl;
    cout << "x.cx address: " << &x.cx << endl;
    cout << "x.staticx address: " << &x.staticx << endl;

    Y y;
    cout << "\nY:" << endl;
    cout << "sizeof(Y) = " << sizeof(Y) << endl;
    cout << "y address: " << &y << endl;
    cout << "y.ay address: " << &y.ay << endl;
    cout << "y.by address: " << &y.by << endl;
    cout << "y.staticy address: " << &y.staticy << endl;

    M m;
    cout << "\nM:" << endl;
    cout << "sizeof(M) = " << sizeof(M) << endl;
    cout << "m address: " << &m << endl;
    cout << "m.ax address: " << &m.ax << endl;
    cout << "m.bx address: " << &m.bx << endl;
    cout << "m.cx address: " << &m.cx << endl;
    cout << "m.ay address: " << &m.ay << endl;
    cout << "m.by address: " << &m.by << endl;
    cout << "m.staticx address: " << &m.staticx << endl;
    cout << "m.staticy address: " << &m.staticy << endl;
    cout << "m.am address: " << &m.am << endl;

    return 0;
}

程序运行结果如下:
在这里插入图片描述
从上述结果可以看出:在M中,首先存放的是最左边的基类(也就是第一个基类)X的子对象的数据成员,然后存放Y的数据成员,最后存放M的数据成员。

M的内存布局如下:
在这里插入图片描述

1.4、虚继承

1.4.1、单一虚继承

#include <iostream>
using namespace std;

class X
{
public:
    int ax = 0;
};

class Y :virtual public X
{
public:
    int ay = 0;
};

class Z :virtual public X
{
public:
    virtual void funz() { cout << "Z::funx()" << endl; }
    int az = 0;
};

int main()
{
    X x;
    cout << "X:" << endl;
    cout << "sizeof(X) = " << sizeof(X) << endl;
    cout << "x address: " << (&x) << endl;
    cout << "x.ax address: " << &x.ax << endl;

    Y y;
    cout << "\nY:" << endl;
    cout << "sizeof(Y) = " << sizeof(Y) << endl;
    cout << "y address: " << (&y) << endl;
    cout << "y.ay address: " << &y.ay << endl;
    cout << "y.ax address: " << &y.ax << endl;

    Z z;
    cout << "\nY:" << endl;
    cout << "sizeof(Z) = " << sizeof(Z) << endl;
    cout << "z address: " << (&z) << endl;
    cout << "z.az address: " << &z.az << endl;
    cout << "z.ax address: " << &z.ax << endl;

    return 0;
}

程序运行结果如下:
在这里插入图片描述
从上述结果可以看出:在Y中,首先存放的是虚基类表指针,然后存放Y中的数据成员,最后存放虚基类X的数据成员。在Z中,首先存放的是虚函数表指针,然后是虚基类表指针,接下来存放Z中的数据成员,最后存放虚基类X的数据成员。

x、y、z的内存布局如下:
在这里插入图片描述

在这里插入图片描述

1.4.2、多重虚继承

#include <iostream>
using namespace std;

class X
{
public:
    int ax = 0;
};

class Y :virtual public X
{
public:
    virtual void funy() { cout << "Y::funy" << endl; }
    int ay = 0;
};

class Z :virtual public X
{
public:
    int az = 0;
};

class M :public Y, public Z
{
public:
    virtual void funm() { cout << "M::funm()" << endl; }
    int am = 0;
};

int main()
{
    X x;
    cout << "X:" << endl;
    cout << "sizeof(X) = " << sizeof(X) << endl;
    cout << "x address: " << (&x) << endl;
    cout << "x.ax address: " << &x.ax << endl;

    Y y;
    cout << "\nY:" << endl;
    cout << "sizeof(Y) = " << sizeof(Y) << endl;
    cout << "y address: " << (&y) << endl;
    cout << "y.ay address: " << &y.ay << endl;
    cout << "y.ax address: " << &y.ax << endl;

    Z z;
    cout << "\nZ:" << endl;
    cout << "sizeof(Z) = " << sizeof(Z) << endl;
    cout << "z address: " << (&z) << endl;
    cout << "z.az address: " << &z.az << endl;
    cout << "z.ax address: " << &z.ax << endl;


    M m;
    cout << "\nM:" << endl;
    cout << "sizeof(M) = " << sizeof(M) << endl;
    cout << "m address: " << (&m) << endl;
    cout << "m.ay address: " << &m.ay << endl;
    cout << "m.az address: " << &m.az << endl;
    cout << "m.am address: " << &m.am << endl;
    cout << "m.ax address: " << &m.ax << endl;

    return 0;
}

程序运行结果如下:
在这里插入图片描述
从上述结果可以看出:在M中,首先存放的是最左边的基类(即第一个基类)的子对象,需要注意的是:M中的虚函数地址存放在Y的子对象的虚函数表中,接下来依次存放其他基类子对象,然后是M的数据成员,最后存放虚基类X的子对象。

x、y、z、m的内存布局如下

在这里插入图片描述
在这里插入图片描述

二、数据成员指针

#include <iostream>
#include <cstdio>
using namespace std;

class X
{
public:
    int ax = 0;
    int bx = 1;

    void printfun()
    {
        printf("&X::ax = %p\n", &X::ax);
        printf("&X::bx = %p\n", &X::bx);
    }
};

int main() 
{
    X x;
    x.printfun();

    return 0;
}

程序运行结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spring_24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值