C++继承模型

继承中的对象模型

问题:从父类继承过来的成员,哪些属于子类对象中?

示例:

#include<iostream>
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;
};

void test01()
{
	cout << "sizeof Son = " << sizeof(Son) << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}


查看继承模型


打开工具窗口后,定位到当前CPP文件的盘符

然后输入: cl /d1 reportSingleClassLayout查看的类名 所属文件名
我输入的是 cl /d1 reportSingleClassLayoutSon "test6.cpp" 因为我要查看的类是Son 操作的文件名是test6.cpp

结论: 父类中私有成员也是被子类继承下去了,只是由编译器给隐藏后访问不到



posted @ 2020-04-11 11:43  Akmf's_blog  阅读(82)  评论(0)    收藏  举报