菱形虚继承遇到多态(虚函数表)

#include <iostream>

using namespace std;


class Base{
public:
	int intBase1 =1;
	Base()
	{
		cout << "Base" << endl;
	}
	virtual void func1()
	{
		cout << "Base_func1" << endl;
	}
	virtual void func2()
	{
		cout << "Base_func2" << endl;
	}
	virtual ~Base()
	{
		cout << "~Base" << endl;
	}
};

class BaseA :virtual public Base{
public:
	int intBaseA1 =1;
	BaseA()
	{
		intBase1 = 2;
		cout << "BaseA" << endl;
	}
	virtual void func1()
	{
		cout << "BaseA_func1" << endl;
	}
	virtual void funcA()
	{
		cout << "BaseA_funcA" << endl;
	}
	
	virtual ~BaseA()
	{
		cout << "~BaseA" << endl;
	}
};
class BaseB :virtual public Base{
public:
	int intBaseB1 = 1;
	BaseB()
	{
		intBase1 = 3;
		cout << "BaseB" << endl;
	}
	virtual void func2()
	{
		cout << "BaseB_func2" << endl;
	}
	virtual void funcB()
	{
		cout << "BaseA_funcB" << endl;
	}
	virtual ~BaseB()
	{
		cout << "~BaseB" << endl;
	}
};

class BaseD :public BaseA, public BaseB{
public:
	int intBaseD1 =1;
	BaseD()
	{
		cout << "BaseD" << endl;
	}
	virtual void func3()
	{
		cout << "BaseD_func3" << endl;
	}

	virtual ~BaseD()
	{
		cout << "~BaseD" << endl;
	}
};

int main(int argc, char* argv[])
{
	BaseD *baseD = new BaseD();
	baseD->func1();
	baseD->func2();
	baseD->func3();
	BaseA *base = baseD;
	Base *base = baseD;
	base->func1();
	base->func2();
	cout << "&baseD    :" <<  baseD << endl;
	cout << "&intBase1 :" <<& baseD->intBase1 << endl;
	cout << "&intBaseA1:" <<& baseD->intBaseA1 << endl;
	cout << "&intBaseB1:" <<& baseD->intBaseB1 << endl;
	cout << "&intBaseD1:" <<& baseD->intBaseD1 << endl;
	delete base;
	system("pause");
	return 0;
}

54 dd 3c 00//vfptr A //0x003cdd54(12byte)
70 dd 3c 00             //0x003cdd70(8byte)
01 00 00 00//intBaseA1
60 dd 3c 00//vfptr B //0x003cdd60(16byte)
78 dd 3c 00            //0x003cdd78(...)
01 00 00 00 //intBaseB1
01 00 00 00 //intBaseD1
f0  df  3c 00 //vfptr base //0x003cdff0
03 00 00 00 //intBase1

我不懂上面的红色的地址里面放的什么,偏移?虚函数表指针都定位出来了,但是对应的虚函数个数,却和想象的不一样。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值