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

#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

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

猜你喜欢

转载自blog.csdn.net/u012516571/article/details/85312794