分享

多重继承下的Virtual Function 内存布局

 goodwangLib 2013-12-03

本文的例子来自<<Inside The C++ Object Model>> P159 ,转发请注明出处,谢谢。

1.先给出测试代码和测试结果

  1. #include<iostream>  
  2. using namespace std;  
  3. class Base1  
  4. {  
  5. public:  
  6.     virtual ~Base1() {};  
  7.     virtual void speakClearly() {cout<<"Base1::speakClearly()"<<endl;}  
  8.     virtual Base1 *clone() const {cout<<"Base1::clone()"<<endl; return new Base1;}  
  9. protected:  
  10.     float data_Base1;  
  11. };  
  12. class Base2  
  13. {  
  14. public:  
  15.     virtual ~Base2() {};  
  16.     virtual void mumble() {cout<<"Base2::mumble()"<<endl;}  
  17.     virtual Base2 *clone() const {cout<<"Base2::clone()"<<endl; return new Base2;}  
  18. protected:  
  19.     float data_Base2;  
  20. };  
  21. class Derived : public Base1,public Base2  
  22. {  
  23. public:  
  24.     virtual ~Derived()  {cout<<"Derived::~Derived()"<<endl;}  
  25.     virtual Derived *clone() const {cout<<"Derived::clone()"<<endl; return new Derived;}  
  26. protected:  
  27.     float data_Derived;  
  28. };  
  29. int main()  
  30. {  
  31.     cout<<sizeof(Base1)<<endl;  
  32.     typedef void (*Fun) ();  
  33.     Fun pFun = NULL;  
  34.     //******************************************************//  
  35.       // vitrual table[0]  
  36.     //******************************************************//  
  37.     cout<<"################ test virtual table[0]   ################"<<endl<<endl;  
  38.     Derived d;  
  39.     cout<<"&d = "<<&d<<endl;  
  40.       
  41.     int *vptr1 = (int*)*((int*)&d+0);  //vptr1为virutal table[0]的地址  
  42.     cout<<"vptr1 = "<<vptr1<<endl;  
  43.     int *pf1 = (int*)*((int*)*((int*)&d+0)+0);   //pf1为virtual table[0]里的第一个虚拟函数Derived::~Derived()的地址  
  44.   //int *pf1 = (int*)*((int*)vptr1[0]);  //与上面等价  
  45.     cout<<"&vptr1[0] = "<<&vptr1[0]<<endl;  
  46.     cout<<"pf1 = "<<pf1<<endl;  
  47.     pf1 = (int*)*((int*)*((int*)&d+0)+1) ;//pf1为virtual table[0]里的第二个虚拟函数Base1::speakClearly()的地址  
  48.     cout<<"&vptr1[1] = "<<&vptr1[1]<<endl;  
  49.     cout<<"pf1 = "<<pf1<<endl;  
  50.     pFun = (Fun)pf1;                         
  51.     pFun();  
  52.     pf1 = (int*)*((int*)*((int*)&d+0)+2) ;//pf1为virtual table[0]里的第三个虚拟函数Derived::clone()的地址  
  53.     cout<<"&vptr1[2] = "<<&vptr1[2]<<endl;  
  54.     cout<<"pf1 = "<<pf1<<endl;  
  55.     pFun = (Fun)pf1;  
  56.     pFun();  
  57.     pf1 = (int*)*((int*)*((int*)&d+0)+3) ;  
  58.     cout<<"&vptr1[3] = "<<&vptr1[3]<<endl;  
  59.     cout<<"pf1 = "<<pf1<<endl;  
  60.     if(pf1 == NULL)  
  61.         cout<<"NUll"<<endl;           //结果表明,virtual table[0][3]为NULL  
  62.     //******************************************************//  
  63.       // vitrual table[1]  
  64.     //******************************************************//  
  65.     cout<<endl<<endl<<"################ test virtual table[1]   ################"<<endl<<endl;  
  66.     int sz = sizeof(Base1)/4;  
  67.     int *vptr2 = (int*)*((int*)&d+sz);  //vptr2为virutal table[1]的地址  
  68.     cout<<"vptr2 = "<<vptr2<<endl;  
  69.     pf1 = (int*)*((int*)*((int*)&d+sz)+0); //pf1为virtual table[1]里的第一个虚拟函数Derived::~Derived()的地址  
  70.     cout<<"&vptr2[0] = "<<&vptr2[0]<<endl;  
  71.     cout<<"pf1 = "<<pf1<<endl;  
  72.     pf1 = (int*)*((int*)*((int*)&d+sz)+1); //pf1为virtual table[1]里的第二个虚拟函数Based::mumble()的地址  
  73.     cout<<"&vptr2[1] = "<<&vptr2[1]<<endl;  
  74.     cout<<"pf1 = "<<pf1<<endl;  
  75.     pFun = (Fun)pf1;  
  76.     pFun();  
  77.     pf1 = (int*)*((int*)*((int*)&d+sz)+2); //pf1为virtual table[1]里的第三个虚拟函数Derived::clone()的地址  
  78.     cout<<"&vptr2[2] = "<<&vptr2[2]<<endl;  
  79.     cout<<"pf1 = "<<pf1<<endl;  
  80.     pFun = (Fun)pf1;  
  81.     pFun();  
  82.     pf1 = (int*)*((int*)*((int*)&d+sz)+3) ;  
  83.     cout<<"&vptr2[3] = "<<&vptr2[3]<<endl;  //??????  
  84.     cout<<"pf1 = "<<pf1<<endl;  
  85.     if(pf1 == NULL)  
  86.         cout<<"NUll"<<endl;           
  87.     pFun = (Fun)pf1;   //  虚函数表里问什么有两个地址指向Derived::clone()  
  88.     pFun();  
  89.     pf1 = (int*)*((int*)*((int*)&d+sz)+4) ;  
  90.     cout<<"&vptr2[4] = "<<&vptr2[4]<<endl;  
  91.     cout<<"pf1 = "<<pf1<<endl;  
  92.     if(pf1 == NULL)  
  93.         cout<<"NUll"<<endl;           //结果表明,virtual table[0][3]为NULL  
  94.     return 0;  
  95. }  

用VS2010测试的结果:

结果分析:

 

2.总结

   1).派生类的虚函数表数目是它所有基类的虚函数数目之和,基类的虚函数表被复制到派生类的对应的虚函数表中。

   2).派生类中重写基类的虚拟函数时,该被重写的函数在派生类的虚函数列表中得到更新,派生类的虚析构函数覆盖基类的虚析构函数。

   3).派生类中新增加的虚函数被添加到与第一个基类相对应的虚函数表中。

   4).测试表明:在VS2010的C++编译器中,虚函数表不总是以NULL结束。

   5).virtual table[1]中的clone分别为:Base2* Derived::clone 和 Derived* Derived::clone 。这里为什么会比table[0]多一个两个Base2* Derived::clone呢?因为:如果将一个Derived对象地址指定给一个Base1指针或者Derived指针是,虚拟机制使用的是virtual table[0] ;如果将一个Derived对象地址指定给一个Base2指针时,虚拟机制使用的是virtual table[1]。 (<<C++对象模型>> P164)

通过命令行 /d1reportSingleClassLayoutDerived 可以看到Derived的内存布局:

 1>  class Derived size(20):
1>   +---
1>   | +--- (base class Base1)
1>   0 | | {vfptr}
1>   4 | | data_Base1
1>   | +---
1>   | +--- (base class Base2)
1>   8 | | {vfptr}
1>  12 | | data_Base2
1>   | +---
1>  16 | data_Derived
1>   +---
1>  
1>  Derived::$vftable@Base1@:
1>   | &Derived_meta
1>   |  0
1>   0 | &Derived::{dtor}
1>   1 | &Base1::speakClearly
1>   2 | &Derived::clone
1>  
1>  Derived::$vftable@Base2@:
1>   | -8
1>   0 | &thunk: this-=8; goto Derived::{dtor}
1>   1 | &Base2::mumble
1>   2 | &thunk: this-=8; goto Base2* Derived::clone
1>   3 | &thunk: this-=8; goto Derived* Derived::clone

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多