C++多重继承虚表的内存分布

时间:2022-01-10 04:56:43

接前面虚表的内存分布,今天重点看多重继承的虚表内存分布,简单的说,继承几个类便有几个虚表,如下代码

class Drive : public Base1, public Base2, public Base3 {
public:
virtual void fd() { cout << "Drive::fd" << endl; }
virtual void gd() { cout << "Drive::gd" << endl; }
};

则虚表有3个,如下图所示:

C++多重继承虚表的内存分布

添加更多的测试代码:

// trytest.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <WTypes.h>
#include < iostream > using namespace std; class Base1 {
virtual void f() { cout << "Base1::f" << endl; }
virtual void g() { cout << "Base1::g" << endl; }
}; class Base2 {
virtual void f() { cout << "Base2::f" << endl; }
virtual void g() { cout << "Base2::g" << endl; }
}; class Base3 {
virtual void f() { cout << "Base3::f" << endl; }
virtual void g() { cout << "Base3::g" << endl; }
}; class Drive : public Base1, public Base2, public Base3 {
public:
virtual void fd() { cout << "Drive::fd" << endl; }
virtual void gd() { cout << "Drive::gd" << endl; }
}; typedef void(*Fun)(void); int main() {
Drive objDrive;
cout << "Size is = " << sizeof(objDrive) << endl; Fun pFun = NULL; cout<<"sizeof(int)="<<sizeof(int)<<endl;
cout<<"sizeof(int*)="<<sizeof(int*)<<endl; // 调用Base1的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用Base1的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Address of virtual pointer 1 " << (int*)(&objDrive + ) << endl;
cout << "Value at virtual pointer i.e. Address of virtual table "
<< (int*)*(int*)((int*)&objDrive + ) << endl;
cout << "Information about VTable 1" << endl;
cout << "Value at 1st entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 2nd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; // 调用Base2的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用Base2的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Address of virtual pointer 2 " << (int*)((int*)&objDrive + ) << endl;
cout << "Value at virtual pointer i.e. Address of virtual table "
<< (int*)*(int*)((int*)&objDrive + ) << endl;
cout << "Information about VTable 2" << endl;
cout << "Value at 1st entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 2nd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; // 调用Base3的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用Base3的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Address of virtual pointer 3 " << (int*)((int*)&objDrive + ) << endl;
cout << "Value at virtual pointer i.e. Address of virtual table "
<< (int*)*(int*)((int*)&objDrive + ) << endl;
cout << "Information about VTable 3" << endl;
cout << "Value at 1st entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 2nd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; // 调用派生类的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用派生类的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Value at 3rd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 4th entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; //明确多重继承中虚函数的内存位置
cout <<"Address at 1st entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; cout <<"Address at 2nd entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; cout <<"Address at 3rd entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; cout <<"Address at 4th entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); return ;
}

内存分布如下所示:

C++多重继承虚表的内存分布