继承中的对象模型分析
知识点:
1.父类中所有非静态成员属性都会被子类继承下去
2.父类中私有成员属性是被编译器隐藏了,因此访问不到,但是确实被继承下去
分析:
从图中可以看到,Son类继承了Base类中private权限的m_c成员变量,只是子类访问不到
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C; //私有成员只是被隐藏了,但是还是会继承下去
};
//公共继承
class Son :public Base
{
public:
int m_D;
};
//利用开发人员命令提示工具查看对象模型
/*
跳转盘符:D:
跳转文件路径 cd 文件路径
查看命名 dir
cl /d1 reprotSingleClassLayout类名 文件名.cpp
*/
void test01()
{
//父类中所有非静态成员属性都会被子类继承下去
//父类中私有成员属性是被编译器给隐藏了,因此是访问不到的,但是确实被继承下去了
cout << "sizeof Son = " << sizeof(Son) << endl;
}
int main() {
test01();
system("pause");
return 0;
}