分享

C++的虚函数表

 astrotycoon 2015-10-22

最近在看COM的聚合问题,发现一篇好文:http://www./xczhang/archive/2008/01/20/41508.html,自己结合OD学习下吧,加深印象

先贴代码:

  1. #include <iostream>  
  2. using namespace std;  
  3. //#include <stdio.h>  
  4.   
  5. class Base  
  6. {  
  7. public:  
  8.     virtual void f()  
  9.     {  
  10.         cout<<"Base::f"<<endl;  
  11.     }  
  12.   
  13.     virtual void g()  
  14.     {  
  15.         cout<<"Base::g"<<endl;  
  16.     }  
  17.   
  18.     virtual void h()  
  19.     {  
  20.         cout<<"Base::h"<<endl;  
  21.     }  
  22. };  
  23.   
  24. typedef void(*Fun)(void);   
  25.   
  26. int main()  
  27. {  
  28.     Base b;   
  29.     Fun pFun = NULL;   
  30.       
  31.     cout << "虚函数表地址:" << (int*)*(int*)(&b) << endl;   
  32.     cout << "虚函数表 — 第一个函数地址:" << (int)*(int*)*(int*)(&b) << endl;   
  33.     // Invoke the first virtual function   
  34.     pFun = (Fun)*((int*)*(int*)(&b));   
  35.     pFun();   
  36.   
  37.     return 0;  
  38. }  


运行Release,注意不要把PDB文件删了,用OD运行,在main函数加个断点,F9,再F8单步调试

  1. 00401090 >/$  A1 3C204000   mov     eax, dword ptr [<&MSVCP90.std::endl>]  
  2. 00401095  |?  8B0D 5C204000 mov     ecx, dword ptr [<&MSVCP90.std::cout>]             ;  MSVCP90.std::cout  
  3. 0040109B  |?  50            push    eax  
  4. 0040109C  |.  68 98214000   push    offset Base::`vftable'  
  5. 004010A1  |.  68 64214000   push    00402164  
  6. 004010A6  |?  51            push    ecx  
  7. 004010A7  |.  E8 84010000   call    std::operator<<<std::char_traits<char> >  
  8. 004010AC  |?  83C4 08       add     esp, 8  
  9. 004010AF  |?  8BC8          mov     ecx, eax  
  10. 004010B1  |?  FF15 48204000 call    dword ptr [<&MSVCP90.std::basic_ostream<char,std:>;  MSVCP90.std::basic_ostream<char,std::char_traits<char> >::operator<<  
  11. 004010B7  |?  8BC8          mov     ecx, eax  
  12. 004010B9  |?  FF15 40204000 call    dword ptr [<&MSVCP90.std::basic_ostream<char,std:>;  MSVCP90.std::basic_ostream<wchar_t,std::char_traits<wchar_t> >::operator<<  
  13. 004010BF  |?  8B15 3C204000 mov     edx, dword ptr [<&MSVCP90.std::endl>]             ;  MSVCP90.std::endl  
  14. 004010C5  |?  A1 98214000   mov     eax, dword ptr [Base::`vftable']  
  15. 004010CA  |?  8B0D 5C204000 mov     ecx, dword ptr [<&MSVCP90.std::cout>]             ;  MSVCP90.std::cout  

在0040109C |. 68 98214000 push offset Base::`vftable'发现虚函数表的地址,调试到此:

可以看到information了

00402198=offset Base::`vftable'
test.cpp:31.  cout << "虚函数表地址:" << (int*)*(int*)(&b) << endl;
之所以能显示test.cpp:31是因为存在符号文件,继续运行,直到出现

这里打印是00402198,和前面的information框显示一致,说明判断没错,看代码知道,这是类对象的地址中的前四个字节内容,保存的是就是虚函数表地址

OK,我们继续,在dump框中ctrl+G输入00402198,跳到如下界面

前面说了,00402198是虚函数表的地址,那么我们现在看到的就是虚函数表的内容,很清楚了吧,三个,

那么这三个地址,我们可以归纳出为00401000/00401030/00401060,那么我们在上面的反汇编窗口找下这三个地址对应的位置吧:

看到用红色框标出的吧,三者的符号文件显示分别在test.cpp的9/14/19行,分别对应base::f,base::g,base::h

所以我们得到这样的结论,对象的地址的内容中前四个字节保存了虚函数表的地址,虚函数表地址的内容中依次保存了虚函数地址

 

下面继续看看

一般继承(无虚函数覆盖)

先贴代码:

  1. #include <iostream>  
  2. using namespace std;  
  3. //#include <stdio.h>  
  4.   
  5. class Base  
  6. {  
  7. public:  
  8.     virtual void f()  
  9.     {  
  10.         cout<<"Base::f"<<endl;  
  11.     }  
  12.   
  13.     virtual void g()  
  14.     {  
  15.         cout<<"Base::g"<<endl;  
  16.     }  
  17.   
  18.     virtual void h()  
  19.     {  
  20.         cout<<"Base::h"<<endl;  
  21.     }  
  22. };  
  23.   
  24. class Derive: public Base  
  25. {  
  26. public:  
  27.     virtual void f1()  
  28.     {  
  29.         cout<<"Derive::f1"<<endl;  
  30.     }  
  31.   
  32.     virtual void g1()  
  33.     {  
  34.         cout<<"Derive::g1"<<endl;  
  35.     }  
  36.   
  37.     virtual void h1()  
  38.     {  
  39.         cout<<"Derive::h1"<<endl;  
  40.     }  
  41. };  
  42.   
  43. int main()  
  44. {  
  45.     Base b;   
  46.     cout << "Base虚函数表地址:" << (int*)*(int*)(&b) << endl;   
  47.     Derive d;  
  48.     cout << "Derive虚函数表地址:" << (int*)*(int*)(&d) << endl;   
  49.   
  50.     return 0;  
  51. }  


过程不重复了,OD贴个图:

可以看出Base的虚表还是没变,而Derive的虚函数表对应于下图:

我们可以看到下面几点:

1)虚函数按照其声明顺序放于表中。

2)父类的虚函数在子类的虚函数前面。

一般继承(有虚函数覆盖)

覆盖父类的虚函数是很显然的事情,不然,虚函数就变得毫无意义。下面,我们来看一下,如果子类中有虚函数重载了父类的虚函数,会是一个什么样子?假设,我们有下面这样的一个继承关系。

为了让大家看到被继承过后的效果,在这个类的设计中,我只覆盖了父类的一个函数:f()

贴代码:

  1. #include <iostream>  
  2. using namespace std;  
  3. //#include <stdio.h>  
  4.   
  5. class Base  
  6. {  
  7. public:  
  8.     virtual void f()  
  9.     {  
  10.         cout<<"Base::f"<<endl;  
  11.     }  
  12.   
  13.     virtual void g()  
  14.     {  
  15.         cout<<"Base::g"<<endl;  
  16.     }  
  17.   
  18.     virtual void h()  
  19.     {  
  20.         cout<<"Base::h"<<endl;  
  21.     }  
  22. };  
  23.   
  24. class Derive: public Base  
  25. {  
  26. public:  
  27.     virtual void f()  
  28.     {  
  29.         cout<<"Derive::f"<<endl;  
  30.     }  
  31.   
  32.     virtual void g1()  
  33.     {  
  34.         cout<<"Derive::g1"<<endl;  
  35.     }  
  36.   
  37.     virtual void h1()  
  38.     {  
  39.         cout<<"Derive::h1"<<endl;  
  40.     }  
  41. };  
  42.   
  43. int main()  
  44. {  
  45.     Base b;   
  46.     cout << "Base虚函数表地址:" << (int*)*(int*)(&b) << endl;   
  47.     Derive d;  
  48.     cout << "Derive虚函数表地址:" << (int*)*(int*)(&d) << endl;   
  49.   
  50.     return 0;  
  51. }  


过程不重复了,OD结合贴个图:

可以看出Base的虚表还是没变,而Derive的虚函数表第一个是00401090,继续OD看下:

test.cpp第28行对应Derive:f函数,所以

Derive的虚函数表如下:

我们从表中可以看到下面几点,

1)覆盖的f()函数被放到了虚表中原来父类虚函数的位置。

2)没有被覆盖的函数依旧。

再改写下main:

  1. int main()  
  2. {  
  3.     //Base b;   
  4.     //cout << "Base虚函数表地址:" << (int*)*(int*)(&b) << endl;   
  5.     //Derive d;  
  6.     //cout << "Derive虚函数表地址:" << (int*)*(int*)(&d) << endl;   
  7.     Base *b = new Derive;  
  8.     cout << "Derive虚函数表地址:" << (int*)*(int*)(b) << endl;   
  9.     return 0;  
  10. }  


继续OD查看:


这样,我们就可以看到对于下面这样的程序,

Base *b = new Derive();

b->f();

由b所指的内存中的虚函数表的f()的位置已经被Derive::f()函数地址所取代,于是在实际调用发生时,是Derive::f()被调用了。这就实现了多态。

多重继承(无虚函数覆盖)

下面,再让我们来看看多重继承中的情况,假设有下面这样一个类的继承关系。注意:子类并没有覆盖父类的函数。

贴个代码:

  1. #include <iostream>  
  2. using namespace std;  
  3. //#include <stdio.h>  
  4.   
  5. class Base1  
  6. {  
  7. public:  
  8.     virtual void f()  
  9.     {  
  10.         cout<<"Base1::f"<<endl;  
  11.     }  
  12.   
  13.     virtual void g()  
  14.     {  
  15.         cout<<"Base1::g"<<endl;  
  16.     }  
  17.   
  18.     virtual void h()  
  19.     {  
  20.         cout<<"Base1::h"<<endl;  
  21.     }  
  22. };  
  23.   
  24. class Base2  
  25. {  
  26. public:  
  27.     virtual void f()  
  28.     {  
  29.         cout<<"Base2::f"<<endl;  
  30.     }  
  31.   
  32.     virtual void g()  
  33.     {  
  34.         cout<<"Base2::g"<<endl;  
  35.     }  
  36.   
  37.     virtual void h()  
  38.     {  
  39.         cout<<"Base2::h"<<endl;  
  40.     }  
  41. };  
  42.   
  43. class Base3  
  44. {  
  45. public:  
  46.     virtual void f()  
  47.     {  
  48.         cout<<"Base3::f"<<endl;  
  49.     }  
  50.   
  51.     virtual void g()  
  52.     {  
  53.         cout<<"Base3::g"<<endl;  
  54.     }  
  55.   
  56.     virtual void h()  
  57.     {  
  58.         cout<<"Base3::h"<<endl;  
  59.     }  
  60. };  
  61.   
  62. class Derive: public Base1,  
  63.               public Base2,  
  64.               public Base3  
  65. {  
  66. public:  
  67.     virtual void f1()  
  68.     {  
  69.         cout<<"Derive::f"<<endl;  
  70.     }  
  71.   
  72.     virtual void g1()  
  73.     {  
  74.         cout<<"Derive::g1"<<endl;  
  75.     }  
  76.   
  77. };  
  78.   
  79. int main()  
  80. {  
  81.     Derive *p = new Derive;  
  82.     cout << "Derive对象的指针地址:" <<p<<endl;  
  83.     cout << "强制转换成Base1的指针地址:"<<(Base1*)p<<endl;  
  84.     cout << "强制转换成Base2的指针地址:"<<(Base2*)p<<endl;  
  85.     cout << "强制转换成Base3的指针地址:"<<(Base3*)p<<endl;  
  86.   
  87.     return 0;  
  88. }  

这时会生成三个虚指针表:

用OD查看下:对于子类实例中的虚函数表,是下面这个样子:

我们可以看到,指针地址的内容依次保存了三个虚函数表的地址,而强制转换成Base1/Base2/Base3地址和这三个虚函数表刚好相对应

我们再看下这三个虚函数表地址分别对应的内容:

 

可以用下图来表示:

 

我们可以看到:

1) 每个父类都有自己的虚表。

2) 子类的成员函数被放到了第一个父类的表中。(所谓的第一个父类是按照声明顺序来判断的)

这样做就是为了解决不同的父类类型的指针指向同一个子类实例,而能够调用到实际的函数。

多重继承(有虚函数覆盖)

下图中,我们在子类中覆盖了父类的f()函数。

 

下面是对于子类实例中的虚函数表的图:

不具体分析了!

我们可以看见,三个父类虚函数表中的f()的位置被替换成了子类的函数指针。这样,我们就可以任一静态类型的父类来指向子类,并调用子类的f()了。如:

Derive d;

Base1 *b1 = &d;

Base2 *b2 = &d;

Base3 *b3 = &d;

b1->f(); //Derive::f()

b2->f(); //Derive::f()

b3->f(); //Derive::f()

b1->g(); //Base1::g()

b2->g(); //Base2::g()

b3->g(); //Base3::g()

安全性

每次写C++的文章,总免不了要批判一下C++。这篇文章也不例外。通过上面的讲述,相信我们对虚函数表有一个比较细致的了解了。水可载舟,亦可覆舟。下面,让我们来看看我们可以用虚函数表来干点什么坏事吧。

一、通过父类型的指针访问子类自己的虚函数

我们知道,子类没有重载父类的虚函数是一件毫无意义的事情。因为多态也是要基于函数重载的。虽然在上面的图中我们可以看到Base1的虚表中有Derive的虚函数,但我们根本不可能使用下面的语句来调用子类的自有虚函数:

Base1 *b1 = new Derive();

b1->f1(); //编译出错

任何妄图使用父类指针想调用子类中的未覆盖父类的成员函数的行为都会被编译器视为非法,所以,这样的程序根本无法编译通过。但在运行时,我们可以通过指针的方式访问虚函数表来达到违反C++语义的行为。(关于这方面的尝试,其实我们很容易从上面的OD看到的虚函数表发现,只要我们能确定这个函数在虚函数表中的地址,我们就能很easy的调用到它)

二、访问non-public的虚函数

另外,如果父类的虚函数是private或是protected的,但这些非public的虚函数同样会存在于虚函数表中,所以,我们同样可以使用访问虚函数表的方式来访问这些non-public的虚函数,这是很容易做到的。

如:

class Base {

private:

virtual void f() { cout << "Base::f" << endl; }

};

class Derive : public Base{

};

typedef void(*Fun)(void);

void main() {

Derive d;

Fun pFun = (Fun)*((int*)*(int*)(&d)+0);

pFun();

}

 

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多