物件記憶體佈局 (7)

weixin_34054866發表於2014-11-12

在物件記憶體佈局 (5)的程式碼中,在Derived類中覆蓋Base1中宣告的vfBase1_1(),其他程式碼不變。修改後的程式碼如下:

#include <iostream>
using namespace std;

class Base1
{
public:
    int m_base1;
    inline virtual void vfBase1_1()
    {
        cout << "This is in Base1::vfBase1_1()" << endl;
    }
    inline virtual void vfBase1_2()
    {
        cout << "This is in Base1::vfBase1_2()" << endl;
    }
};
class Base2
{
public:
    int m_base2;
    inline virtual void vfBase2_1()
    {
        cout << "This is in Base2::vfBase2_1()" << endl;
    }
    inline virtual void vfBase2_2()
    {
        cout << "This is in Base2::vfBase2_2()" << endl;
    }
};
class Base3
{
public:
    int m_Base3;
    inline virtual void vfBase3_1()
    {
        cout << "This is in Base3::vfBase3_1()" << endl;
    }
    inline virtual void vfBase3_2()
    {
        cout << "This is in Base3::vfBase3_2()" << endl;
    }
};
class Derived : public Base1, public Base2, public Base3
{
public:
    int m_derived;
    inline virtual void fd()
    {
        cout << "This is in Derived::fd()" << endl;
    }
    inline void vfBase1_1()
    {
        cout << "This is in Derived::vfBase1_1()" << endl;
    }
};
typedef void (*VFun)(void);
template<typename T>
VFun virtualFunctionPointer(T* b, int i)
{
    return (VFun)(*((int*)(*(int*)b) + i));
}
int main(void)
{
    Derived d;
    cout << "The size of Derived object = \t" << sizeof(Derived) << endl;
    cout << endl;
    cout << "1st virtual function table: " << endl;
    int i = 0;
    while(virtualFunctionPointer(&d, i)&&i<3)
    {
        VFun pVF = virtualFunctionPointer(&d, i++);
        pVF();
    }
    cout << endl;
    cout << "2nd virtual function table: " << endl;
    i = 0;
    //以32字長的機器,找到下一個繼承base class的vptr
    int* tmp = ((int*)&d)+sizeof(Base1)/4;
    //虛擬函式表中的虛擬函式後面不為NULL?如果不加i的限制會出現段錯誤,不能結束迴圈
    while(virtualFunctionPointer(tmp, i)&&i<2)

    {
        VFun pVF = virtualFunctionPointer(tmp, i++);
        pVF();
    }
    cout << endl;
    cout << "3rd virtual function table: " << endl;
    i = 0;
    tmp = ((int*)&d) + sizeof(Base2)/4;
    while(virtualFunctionPointer(tmp, i)&&i<2)
    {
        VFun pVF = virtualFunctionPointer(tmp, i++);
        pVF();
    }
    return 0;
}

執行結果:

Derived物件值memory layout圖解如下:

 

我們可以看到,在Derived中overriden的虛擬函式Derived::vfBase1_1排在第一個虛擬函式表的第一位。虛擬函式Base::vfBase1_1()由於已經被overridden,所以在Derived物件的虛擬函式表中不再出現。

 

相關文章