對象記憶體布局 (16)

來源:互聯網
上載者:User

前篇: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,理由是這樣做會是問題變得非常複雜。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.