// 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);
}
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
8
4
8
4
为什么会这样?基类的私有数据成员不会继承到派生类中,那么为什么我得到8个字节,派生的大小和o1?