對象記憶體布局 (14)

來源:互聯網
上載者:User

前篇:http://blog.csdn.net/pathuang68/archive/2009/04/24/4105810.aspx

 

繼續探討虛基類對對象記憶體布局的影響。幾個類的繼承關係如,這是虛基類最為常見的用法之一:

 

代碼如下:

#include <iostream>

using namespace std;

 

class Base

{

public:

         int base_member;

};

 

class Derived1 : public virtual Base

{

public:

         int derived1_member;

};

 

class Derived2 : public virtual Base

{

public:

         int derived2_member;

};

 

class ChildDerived : public Derived1, public Derived2

{

public:

         int childderived_member;

};

 

 

int main(void)

{

         ChildDerived cd;

 

         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 << "Derived1 subobject's vbptr = " << (unsigned long*)&cd << endl;

         cout << "Derived2 subobject's vbptr = " << (unsigned long*)&cd + 2 << endl;

 

         cout << endl;

 

         cout << "Address of virtual base class table of Derived1 object = " << (unsigned long*)(*((unsigned long*)&cd)) << endl;

         cout << "Item 1 in Derived1's virtual base class table = " << *((unsigned long*)*(unsigned long*)(&cd) + 0) << endl;

         cout << "Item 2 in Derived1's virtual base class table = " << *((unsigned long*)*(unsigned long*)(&cd) + 1) << endl;

         cout << "Item 3 in Derived1's virtual base class table = " << *((unsigned long*)*(unsigned long*)(&cd) + 2) << endl;

 

         cout << endl;

 

         cout << "Address of virtual base class table of Derived2 object = " << (unsigned long*)*((unsigned long*)&cd + 2) << endl;

         cout << "Item 1 in Derived2's virtual base class table = " << *((unsigned long*)*((unsigned long*)&cd + 2) + 0) << endl;

         cout << "Item 2 in Derived2's virtual base class table = " << *((unsigned long*)*((unsigned long*)&cd + 2) + 1) << endl;

         cout << "Item 3 in Derived2's virtual base class table = " << *((unsigned long*)*((unsigned long*)&cd + 2) + 2) << endl;

 

         cout << endl;

 

         cout << "The address of base class Derived1's instance = " << (Derived1*)&cd << endl;

         cout << "The address of base class Derived2's instance = " << (Derived2*)&cd << endl;

         cout << "The address of base class Base's instance = /t" << (Base*)&cd << endl;

 

         return 0;

}

 

運行結果如下:

 

sizeof(Base) = 4 bytes,這個是很好解釋的,因為Base中由一個類型為int的成員變數。

sizeof(Derived1) = 12 bytes和sizeofDerived2) = 12 bytes,圖解如下:

 

兩個“?”到底是多少我們且不去管它,等我們討論ChildDerived對象的memory layout時再進行詳細說明。從可以看到,Base subobject在最後。

 

現在我們來看看ChildDerived對象的memory layout的情況:

由於Derived1和Derived2都是從Base虛擬繼承而來,因而Base是它們的虛基類,編譯器在編譯的時候會分別給他們安插vbptr指標;Derived1和Derived2同時被ChildDerived普通繼承(非虛繼承),根據C++標準的要求,基類在在衍生類別中保證其原始的完整性,因此兩個vbptr被繼承到了ChildDerived類;由於Base被虛繼承,可以看到Base suboject只有一份拷貝,而且放在最後。

 

從下面圖解可以很清楚地發現:

sizeof(ChildDerived) = 24 bytes;

虛基類執行個體地址(0x0012FF7C) = Derived1的vbptr (0x0012FF68) + Derived1的vbptr到虛基類執行個體地址的位移量(24);

虛基類執行個體地址(0x0012FF7C) = Derived2的vbptr (0x0012FF70) + Derived1的vbptr到虛基類執行個體地址的位移量(12)

 

後篇:http://blog.csdn.net/pathuang68/archive/2009/04/24/4105902.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.