前篇:http://blog.csdn.net/pathuang68/archive/2009/04/24/4105902.aspx
下面讨论虚基类和虚函数同时存在的时候,对对象内存布局的影响。
假定各个类之间的关系如下图:
Base中声明了一个虚函数vfBase()和一个整形成员变量;
Derived1 override了Base中声明的虚函数vfBase(),声明了一个虚函数vfDerived1(),另有一个整形成员变量derived1_member;
Derived2 override了Base中声明的虚函数vfBase(),声明了一个虚函数vfDerived2(),另有一个整形成员变量derived2_member;
ChildDerived分别override了Base、Derived1和Derived2中声明的虚函数vfBase()、vfDerived1() 和vfDerived2(),另外有一个整形成员变量childderived_member;
代码如下:
#include <iostream>
using namespace std;
typedef void (*VFun)(void);
template<typename T>
VFun virtualFunctionPointer(T* b, int i)
{
return (VFun)(*((int*)(*(int*)b) + i));
}
template<typename T>
int virtualBaseTableOffset(T* b, int i)
{
return (int)*((int*)*(int*)b + i);
}
class Base
{
public:
int base_member;
inline virtual void vfBase()
{
cout << "This is in Base::vfBase()" << endl;
}
};
class Derived1 : public virtual Base
{
public:
int derived1_member;
inline void vfBase()
{
cout << "This is in Derived1::vfBase()" << endl;
}
inline virtual void vfDerived1()
{
cout << "This is in Derived1::vfDerived1()" << endl;
}
};
class Derived2 : public virtual Base
{
public:
int derived2_member;
inline void vfBase()
{
cout << "This is in Derived2::vfBase()" << endl;
}
inline virtual void vfDerived2()
{
cout << "This is in Derived2::vfDerived2()" << endl;
}
};
class ChildDerived : public Derived1, public Derived2
{
public:
int childderived_member;
inline void vfBase()
{
cout << "This is in ChildDerived::vfBase()" << endl;
}
inline void vfDerived1()
{
cout << "This is in ChildDerived::vfDerived1()" << endl;
}
inline void vfDerived2()
{
cout << "This is in ChildDerived::vfDerived2()" << endl;
}
};
int main(void)
{
ChildDerived cd;
VFun pVF;
int* tmp;
cout << "sizeof(Base) = /t/t" << sizeof(Base) << endl;
cout << "sizeof(Derived1) = /t" << sizeof(Derived1) << endl;
cout << "sizeof(Derived2) = /t" << sizeof(Derived2) << endl;
cout << "sizeof(ChildDerived) = /t" << sizeof(ChildDerived) << endl;
cout << endl;
cout << "address of ChildDerived object:" << endl;
cout << "address = " << (int*)&cd << endl;
cout << endl;
cout << "1st virtual function table: " << endl;
pVF = virtualFunctionPointer(&cd, 0);
pVF();
cout << endl;
cout << "1st virtual base table: " << endl;
tmp = (int*)((int*)&cd + 1);
cout << "address = " << tmp << endl;
cout << virtualBaseTableOffset(tmp, 0) << "/t<- not sure yet, but it doesn't matter here." << endl;
cout << virtualBaseTableOffset(tmp, 1) << "/t<- offset from Derived1 subobject's vbptr to Base subobject." << endl;
cout << virtualBaseTableOffset(tmp, 2) << "/t<- means the end of this virtual base table." << endl;
cout << endl;
tmp = ((int*)&cd) + 3;
cout << "2nd virtual function table: " << endl;
pVF = virtualFunctionPointer(tmp, 0);
pVF();
cout << endl;
cout << "2nd virtual base table: " << endl;
tmp = (int*)((int*)&cd + 4);
cout << "address = " << tmp << endl;
cout << virtualBaseTableOffset(tmp, 0) << "/t<- not sure yet, but it doesn't matter here." << endl;
cout << virtualBaseTableOffset(tmp, 1) << "/t<- offset from Derived2 subobject's vbptr to Base subobject." << endl;
cout << virtualBaseTableOffset(tmp, 2) << "/t<- means the end of this virtual base table." << endl;
cout << endl;
tmp = ((int*)&cd) + 7;
cout << "3rd virtual function table: " << endl;
pVF = virtualFunctionPointer(tmp, 0);
pVF();
cout << endl;
cout << "Derived1 subobject address = /t" << (Derived1*)&cd << endl;
cout << "Derived2 subobject address = /t" << (Derived2*)&cd << endl;
cout << "Base subobject address = /t" << (Base*)&cd << endl;
return 0;
}
运行结果如下:
ChildDerived、Derived1和Derived2对象memory layout分别图解如下:
两个虚基类偏移量图解如下(虚函数表和虚基类表略):
结论:
其一,只要涉及到虚基类,一切问题就变得复杂起来;
其二,如果同时存在vfptr和vbptr,vfptr居前,vbptr居后;
其三,普通基类居前,虚基类总是尽可能地排列在layout的最后;
其四,两个同一层次的虚基类subobject,先声明者居前,后声明者居后,这点和普通基类是一样的;
其五,两个不同层次的虚基类subobject,层次高者居前,层次低者居后;
其六,Stan Lippman建议,不要在一个virtual base class中声明nonstatic data member,理由是这样做会是问题变得非常复杂。