C++ 内存分析(2)

(2)一般多继承(不考虑菱形继承的多继承)

  •     若子类新增虚函数,放在声明的第一个父类的虚函数表中;
  •     若子类重写了父类的虚函数,所有父类的虚函数表都要改变:如BaseVirtualFun1()
  •     内存布局中,父类按照其声明顺序排列。

//.h

//基类2
class FBase2
{
public:
	char m_cBase2Memeber;

public:
	virtual void Base2VirtualFun1() { std::cout << "FBase2::BaseVirtualFun1()\n"; }
	virtual void Base2VirtualFun2() { std::cout << "FBase2::BaseVirtualFun2()\n"; }
};

//一般多继承
class MultiInheritDerive : public FBase, public FBase2
{
public:
	char m_cMultiInheritMemeber;

public:
	//重写 基类1虚函数
	virtual void BaseVirtualFun1() override { std::cout << "MultiInheritDerive::BaseVirtualFun1()\n"; }
	//重写 基类2虚函数
	virtual void Base2VirtualFun1() override { std::cout << "MultiInheritDerive::Base2VirtualFun1()\n"; }
	//新增虚函数
	virtual void VirtualFun3() { std::cout << "MultiInheritDerive::VirtualFun3()\n"; }
};

//.cpp 

//函数指针
typedef void(*pFun)();

void MultiInherit_Test()
{
	std::cout << std::endl << std::endl << "------------------------------------MultiInherit_Test--------------------------------Start" << std::endl;
	MultiInheritDerive MultiInherit;
	MultiInherit.m_cBaseMemeber = 5;
	MultiInherit.m_cBase2Memeber = 6;
	MultiInherit.m_cMultiInheritMemeber = 7;

	//结构大小
	STREXE_D(sizeof(MultiInherit));//x64-19;	x86-11;
	long lV_ptr = *(long *)&MultiInherit;
	long long llV_ptr = *(long long*)&MultiInherit;
	//首地址
	std::cout << "VPtr地址l:" << std::hex << lV_ptr << std::endl;
	std::cout << "VPtr地址ll:" << std::hex << llV_ptr << std::endl;

	//Base Function
	pFun* F1 = nullptr;	
	//Base2 Function
	pFun* Base2F1 = nullptr;
	
#ifdef _WIN64
	F1 = (pFun*)llV_ptr;
	long long * BaseMemeberAddress = (long long *)((char*)(&MultiInherit) + sizeof(long long));
	long long* llBase2V_ptr = (long long*)(((char*)BaseMemeberAddress + 1));
	Base2F1 = (pFun*)(*llBase2V_ptr);
	long long* Base2Memeber = (long long*)((char*)llBase2V_ptr + sizeof(long long));	
#else 
#ifdef _WIN32
	F1 = (pFun*)lV_ptr;
	long * BaseMemeberAddress =(long *)((char*)(&MultiInherit) + sizeof(long));
	long* lBase2V_ptr = (long*)(((char*)BaseMemeberAddress + 1));
	Base2F1 = (pFun*)(*lBase2V_ptr);
	long* Base2Memeber = (long*)((char*)lBase2V_ptr + sizeof(long));
#endif
#endif
	//Base函数调用
	std::cout << "Base函数调用:--------------------------------"<< std::endl;
	(*F1)();
	(*(F1 + 1))();
	(*(F1 + 2))();
	//成员m_cBaseMemeber
	std::cout << "成员m_cBaseMemeber:--------------------------------" << std::endl;
	STREXE_D(&(MultiInherit.m_cBaseMemeber));
	STREXE_D(MultiInherit.m_cBaseMemeber);
	STREXE_D((char)(*BaseMemeberAddress));
	//Base2函数调用
	std::cout << "Base2函数调用:--------------------------------"<< std::endl;
	(*Base2F1)();
	(*(Base2F1+1))();
	//成员m_cBase2Memeber
	std::cout << "成员m_cBase2Memeber:--------------------------------" << std::endl;
	STREXE_D(&(MultiInherit.m_cBase2Memeber));
	STREXE_D(MultiInherit.m_cBase2Memeber);
	STREXE_D((char)(*Base2Memeber));
	std::cout << "成员m_cMultiInheritMemeber:--------------------------------" << std::endl;
	//m_cMultiInheritMemeber
	STREXE_D(&(MultiInherit.m_cMultiInheritMemeber));
	STREXE_D(MultiInherit.m_cMultiInheritMemeber);
	STREXE_D((char)(*((char*)Base2Memeber+1)));
	std::cout << "------------------------------------MultiInherit_Test--------------------------------End" << std::endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值