(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;
}