【发布时间】:2020-12-19 12:29:09
【问题描述】:
这里基类的数据成员是私有的。请解释这段代码。为什么这个程序的输出是80。
#include<iostream>
using namespace std;
class base {
int arr[10];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}
【问题讨论】:
-
您是否希望私人会员比其他会员占用更少的空间?
-
@Botje 这里没有钻石。
derived只有两个base类型的子对象 -
您期望的输出是什么?为什么你认为
80会是个问题? -
@user7860670 继承图看起来像一个菱形,OP有the C++ faq中列出的确切问题
标签: c++ inheritance