多态-虚函数表

VS的对象内存分析:

/d reportSingleClassLayout+类名

 使用方法:项目 -- 属性 -- C/C++ -- 命令行--其他选型(D) 添加命令.如图所示:

726c91b663964261b0bfe902afe673e8.png

Father类:

class Father {
public:
	virtual void Func1() { cout << "Father::Func1" << endl; }
	virtual void Func2() { cout << "Father::Func2" << endl; }
	virtual void Func3() { cout << "Father::Func3" << endl; }
	void Func4() { cout << "Father::Func4" << endl; }
public:
	int x = 200;
	int y = 300;
	static int z;
};
int Father::z = 0;

Father的内存分析: 

/d reportSingleClassLayoutFather

 

Son类: public Father

class Son:public Father
{
public:
	void Func1() { cout << "Son::Func1" << endl; }
	virtual void Func5() { cout << "Son::Func5" << endl; }

private:
	int m = 400;
	int n = 500;
};

Son类的内存分析: 

/d reportSingleClassLayoutSon

 

 子类的虚函数表构建过程:

(1)直接复制父类的虚函数表

(2)如果子类重写了父类的某个虚函数,那么就在这个虚函数表中进行相应的替换

 

(3)如果子类中添加了新的虚函数,就把这个虚函数添加到虚函数的表中(在尾部添加)

 

 注意:所有子类共用同一张虚函数表

main函数:

int main(void) {
	Son son;
	Father* father = &son;
	cout << "----Father* father = &son----" << endl;
	son.Func1();
	son.Func2();
	son.Func3();
	son.Func4();
	son.Func5();
	Father* son2 = new Son();
	cout << "----Father* son2 = new Son()----" << endl;
	son2->Func1();
	son2->Func2();
	son2->Func3();
	son2->Func4();
	//son2->Func5();//错误使用
	return 0;
}

结果:

 全部代码(测试):

#include<iostream>
using namespace std;
//vs的对象内存分析:/d1 reportSingleClassLayout+类名
class Father {
public:
	virtual void Func1() { cout << "Father::Func1" << endl; }
	virtual void Func2() { cout << "Father::Func2" << endl; }
	virtual void Func3() { cout << "Father::Func3" << endl; }
	void Func4() { cout << "Father::Func4" << endl; }
public:
	int x = 200;
	int y = 300;
	static int z;
};
int Father::z = 0;

class Son:public Father
{
public:
	void Func1() { cout << "Son::Func1" << endl; }
	virtual void Func5() { cout << "Son::Func5" << endl; }

private:
	int m = 400;
	int n = 500;
};



typedef void(*func)(void);

void test(void) {
	Father father;
	cout << "sizeof(father): " << sizeof(father) << endl;
	cout << "(int*)&father: " << (int*)&father << endl;
	cout << "&father: " << &father << endl;
	int* vptr = (int*)*(int*)(&father);
	cout << "调用虚函数Func1:" << endl;
	((func) * (vptr + 0))();
	cout << "调用虚函数Func2:" << endl;
	((func) * (vptr + 1))();
	cout << "调用虚函数Func3:" << endl;
	((func) * (vptr + 2))();

	cout << "father.x地址: " << endl;
	cout << &father.x << endl;
	cout << (int*)((int)&father + 4) << endl;
	cout << "father.x:" << endl;
	cout << father.x << endl;
	cout << *(int*)((int)&father + 4) << endl;

	cout << "father.y地址: " << endl;
	cout << &father.y << endl;
	cout << (int*)((int)&father + 8) << endl;
	cout << "father.y:" << endl;
	cout << father.y << endl;
	cout << *(int*)((int)&father + 8) << endl;
}

int main(void) {
	test();
	Son son;
	Father* father = &son;
	cout << "----Father* father = &son----" << endl;
	son.Func1();
	son.Func2();
	son.Func3();
	son.Func4();
	son.Func5();
	Father* son2 = new Son();
	cout << "----Father* son2 = new Son()----" << endl;
	son2->Func1();
	son2->Func2();
	son2->Func3();
	son2->Func4();
	//son2->Func5();//错误使用
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值