小编Anu*_*aag的帖子

为什么具有虚拟继承的派生类的大小会出乎意料?

由于空类的大小是1个字节,但是这个空类实际上是继承的(在gcc编译器上),答案sizeof(derived1)是8字节......,为什么会发生这种情况,sizeof(derived1)必须sizeof(char)+sizeof(virtual pointer)是1+ 4..ie 5字节.所以,从这个额外的3个字节到来的地方...... ???

#include <iostream>
using namespace std;

class Empty
{};

class Derived1 : virtual public Empty<br>
{
    char c;
};

int main()
{
    cout << "sizeof(Empty) " << sizeof(Empty) << endl;
    cout << "sizeof(Derived1) " << sizeof(Derived1) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

2
推荐指数
1
解决办法
301
查看次数

标签 统计

c++ ×1

inheritance ×1