// inheritence experiment
#include"stdafx.h"
#include<iostream>
using namespace std;
class base
{
private:
int i;
};
class derived: public base
{
private:
int j;
};
int main()
{
cout << endl << sizeof(derived) << endl << sizeof(base);
derived o1;
base o2;
cout << endl << sizeof(o1) << endl << sizeof(o2);
}
我得到这样的输出:基类中的私有元素是否添加到派生类的大小?
,为什么是这样呢?基类的私有数据成员不会继承到派生类中,所以为什么我得到两个8字节,派生和o1的大小?
如果你无法访问它们,它们在派生类中的位置是什么? – Ghost
@Ghost:你不能访问它们,但是你的继承函数可能会被访问,并且基础中的函数将被访问。 –
他们可以间接访问!正如我写的,基类中的行为(即函数)可能取决于那些私有成员。而这些功能可能是公开的或保护... – ltjax