飞道的博客

C++ 类地址空间学习

260人阅读  评论(0)
#include <iostream>
using std::cout;
using std::endl;


class Base1 {
public:
	Base1() :a_(100) {}
	virtual void fun1() { cout << "Base1.fun1" << endl; }
	virtual void fun2() { cout << "Base1.fun2" << endl; }
private:
	int a_;
};
class Base2 {
public:
	Base2() :b_(200) {}
	virtual void fun3() { cout << "Base2.fun3" << endl; }
	virtual void fun4() { cout << "Base2.fun4" << endl; }
private:
	int b_;
};
class Derived : public Base1, public Base2 {
public:
	Derived() :d_(300) {}
	virtual void fun1() override { cout << "Derived.fun1" << endl; }
	virtual void fun2() override { cout << "Derived.fun2" << endl; }
	virtual void fun3() override { cout << "Derived.fun3" << endl; }
	virtual void fun4() override { cout << "Derived.fun4" << endl; }
private:
	int d_;
};

int main()
{
	Derived derived;
	cout << (int *)&derived << endl;
	getchar();
	return 0;
}

下图是VS2017的调试窗口显示的相关地址信息

derived地址空间分布图


转载:https://blog.csdn.net/dyingfair/article/details/104599692
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场