虚函数调用过程(用汇编和几个例子解释)

1.(mov ecx,dword ptr [ebp-0Ch])将this指针压入ecx

2.(mov edx,dword ptr [ecx])this指针指向该对象的首地址,而该处的前四个字节存放着该对象的虚函数表的首地址,将虚表指针放到edx中。

3.(call dword ptr [edx+4])由于edx中存放着虚表指针,则edx+4表示调用该虚表中的第二个函数

4.执行到上述操作后,执行该条指令(jmp B::say (00401320)),从而真正调用我们的虚函数!

 

   如果我们的程序是通过指向对象的指针或者是引用来调用该对象的虚函数,则在调用虚函数的过程需要查表(虚函数表)来调用真正的函数。

   调用的不是虚函数则不需要查表,在编译时即可确定调用的是那个函数。

   如果是通过对象来调用则对任何类型的函数都不需要查表。

   虚函数指针是在对象的构造函数中初始化的。

 

关于虚表指针的测试程序:

 

[cpp] view plain copy
 
  1. //virtual1.cpp   
  2. #include <iostream>   
  3.  using namespace std;   
  4.  class A   
  5.  {        
  6.     private:            
  7.     int a_val;        
  8.     public:            
  9.     virtual void show(){cout<<"show"<<a_val<<endl;}            
  10.     virtual void say(){cout<<"say"<<endl;}          
  11.     inline void setVal(int val){a_val = val;}   
  12. };  
  13. int main()   
  14.  {       
  15.     A a;  
  16.     a.setVal(123);  
  17.   
  18.     //受保护的虚函数是不能被直接调用的      
  19.     //而通过虚表指针的地址拷贝则可实现函数的调运       
  20.     //a.show();      
  21.      //该指针存放虚表地址       
  22.      int *des = new int;       
  23.      //该指针存放虚表中的第一个函数       
  24.      int *ptr = new int;       
  25.      memcpy(des,&a,4);      
  26.      memcpy(ptr,reinterpret_cast<int *>(*des),4);       
  27.      void (*pshow)() = reinterpret_cast<void (*)()>(*ptr);       
  28.      //依据__thiscall的调用约定将this指针传入ecx       
  29.      //从而使虚函数能够正确地取出参数       
  30.      int addre = reinterpret_cast<int>(&a);       
  31.      __asm34     {          
  32.         mov ecx,addre  
  33.     };  
  34.        
  35.     pshow();       
  36.     //获得私有的成员v_val  
  37.     memcpy(des, reinterpret_cast<int *>(&a)+1, 4);  
  38.     cout<<*des<<endl;  
  39.     return 0;  
  40. }  
  41.   
  42.   
  43.   
  44. //virtual2.cpp  
  45. #include <iostream>   
  46. #include <string>   
  47. using namespace std;   
  48. class A   
  49. {   
  50.     private:   
  51.     int a_val;   
  52.       
  53.     public:   
  54.     virtual void show(std::string str)  
  55.     {  
  56.         /* 
  57.         int addr = reinterpret_cast<int>(this); 
  58.         __asm14             { 
  59.         mov ecx,addr16             } 
  60.         */  
  61.         cout<<a_val<<endl;  
  62.         cout<<str<<endl;  
  63.     }  
  64.     virtual void say(){cout<<"say"<<endl;}  
  65.     inline void setVal(int val){a_val = val;}  
[cpp] view plain copy
 
  1. };  
  2.     /* 
  3. class B: public A 
  4.     private: 
  5.     int b_val; 
  6.     public: 
  7.     void say(){cout<<"B in say"<<endl;} 
  8.     virtual void hello(){cout<<"hello"<<endl;} 
  9. }; 
  10.     */  
  11. int main()  
  12. {  
  13.     A a;  
  14.     a.setVal(123);  
  15.     //受保护的虚函数是不能被直接调用的  
  16.     //而通过虚表指针的地址拷贝则可实现函数的调运  
  17.     //a.show();  
  18.     //该指针存放虚表地址  
  19.     int *des = new int;  
  20.     //该指针存放虚表中的第一个函数  
  21.     int *ptr = new int;  
  22.     memcpy(des,&a,4);  
  23.     memcpy(ptr,reinterpret_cast<int *>(*des),4);  
  24.     void (*pshow)(std::string) = reinterpret_cast<void (*)(std::string)>(*ptr);  
  25.     int addre = reinterpret_cast<int>(&a);  
  26.     __asm55     {  
  27.     mov ecx,addre57     };  
  28.     string str("hello world");  
  29.     pshow(str);  
  30.     __asm63     {  
  31.     sub esp,10h65     };  
  32.     //获得私有的成员v_val  
  33.     memcpy(des, reinterpret_cast<int *>(&a)+1, 4);  
  34.     cout<<*des<<endl;  
  35.     //cout<<*des<<endl;  
  36.     //cout<<*ptr<<endl;  
  37.     return 0;  
  38. }  
  39.   
  40.   
  41.   
  42. //virtual3.cpp  
  43.   
  44. #include <iostream>   
  45. #include <string>   
  46. using namespace std;   
  47. class A   
  48. {   
  49.     private:   
  50.     char a_val;  
  51.     public:   
  52.     virtual void show()  
  53.     {  
  54.         cout<<"show"<<endl;  
  55.     }  
  56.     virtual void say(){cout<<"say"<<endl;}  
  57.     inline void seta_Val(char val){a_val = val;}  
  58.     inline char geta_Val()const{return a_val;}      
  59. };  
  60. class B: public A19 {  
  61. private:  
  62.     int b_val;  
  63. public:  
  64.     void say(){cout<<"B in say"<<endl;}  
  65.     virtual void hello(){cout<<"hello"<<endl;}  
  66.     inline void setb_Val(int val){b_val = val;}  
  67.     inline int getb_Val()const{return b_val;}  
  68. };  
  69. int main()  
  70. {  
  71.     B b;  
  72.     b.setb_Val(123);  
  73.     b.seta_Val('A');  
  74.     int *vfptr = new int;  
  75.     int *pf = new int;  
  76.     memcpy(vfptr, &b, 4);  
  77.     memcpy(pf, reinterpret_cast<int *>(*vfptr)+2, 4);  
  78.     void (*pfun)() = reinterpret_cast<void (*)()>(*pf);  
  79.     pfun();  
  80.     char *pa_val = new char;  
  81.     int *pb_val = new int;  
  82.     memcpy(pa_val, reinterpret_cast<int *>(&b)+1, sizeof(char));  
  83.     memcpy(pb_val, reinterpret_cast<int *>(&b)+2, sizeof(int));  
  84.     cout<<*pa_val<<endl;  
  85.     cout<<*pb_val<<endl;  
  86.     cout<<"<<<<<<<<<<<<<<"<<endl;  
  87.     *pa_val = 'B';  
  88.     *pb_val = 999;  
  89.     memcpy(reinterpret_cast<int *>(&b)+1, pa_val, sizeof(char));  
  90.     memcpy(reinterpret_cast<int *>(&b)+2, pb_val, 4);  
  91.     cout<<b.geta_Val()<<endl;  
  92.     cout<<b.getb_Val()<<endl;  
  93.     return 0;  
  94. }  



 

由以上测试程序可以得出以下结论:

1.c++对象(基类对象)的内存布局是:对象的内存地址(&a)所指向的内存中的前四个字节中存放的是该对象的虚函数表的首地址(前提是该对象有虚函数),接下来的内存中依次存放该对象的数据成员(非静态的数据成员)。

 

注意:对象的虚函数表中存放的实际上并不是虚函数的入口地址,而是一个跳转指令(jmp)的地址,该跳转指令,转向虚函数的入口,为了叙述方便,我这里作出约定:我们就认为虚函数表中就存放的是虚函数的入口地址。

虚函数的存放顺序与函数的声明顺序是相同的。

 

2.派生类的对象的内存布局是:前四个字节依然存放虚表指针,虚表中首先存放父类的虚函数地址,注意,由于派生类中也可能有①自己的虚函数,同时派生类也可以②重写父类的虚函数,虚函数表的分布如何:

对于情况一而言,将派生类新增加的虚函数地址依次添加到虚表(虚表中已经有父类的虚函数地址)的后面。

对于情况二而言,如果派生类重写了父类的虚函数,则将重写后的虚函数地址替换掉父类原来的虚函数地址,如果没有重写,则按照父类的虚表顺序存放虚函数地址

 

接下来的内存中依次存放该对象的父类的数据成员(非静态的数据成员),然后再存放派生类自己的数据成员。(还有内存对齐的问题)

[cpp] view plain copy
 
    1. #include <iostream>   
    2. #include <string>   
    3. using namespace std;   
    4. class A   
    5. {   
    6. private:   
    7.     char a_val;  
    8.     int a_val2;   
    9. public:  
    10.     virtual void show(){cout<<"show"<<endl;}  
    11.     virtual void say(){cout<<"say"<<endl;}  
    12.     inline void seta_Val(char val){a_val = val;}  
    13.     inline void seta_Val2(int val2){a_val2 = val2;}  
    14.     inline char geta_Val()const{return a_val;}  
    15.     inline int geta_Val2()const{return a_val2;}  
    16. };  
    17. class B: public A21 {  
    18. private:  
    19.     int b_val;  
    20.     char b_val2;  
    21. public:  
    22.     void say(){cout<<"B in say"<<endl;}  
    23.     virtual void hello(){cout<<"hello"<<endl;}  
    24.     inline void setb_Val(int val){b_val = val;}  
    25.     inline void setb_Val2(char val2){b_val2 = val2;}  
    26.     inline int getb_Val()const{return b_val;}  
    27.     inline char getb_Val2()const{return b_val2;}  
    28.      };  
    29.      int main()  
    30.      {  
    31.      B b;  
    32.      b.seta_Val('A');  
    33.      b.seta_Val2(1);  
    34.      b.setb_Val(2);  
    35.      b.setb_Val2('B');  
    36.      int *vfptr = new int;  
    37.      int *pf = new int;  
    38.      memcpy(vfptr, &b, 4);  
    39.      memcpy(pf, reinterpret_cast<int *>(*vfptr)+2, 4);  
    40.      void (*pfun)() = reinterpret_cast<void (*)()>(*pf);  
    41.      pfun();  
    42.      char *pa_val = new char;  
    43.      int *pb_val = new int;  
    44.      memcpy(pa_val, reinterpret_cast<int *>(&b)+1, sizeof(char));  
    45.      memcpy(pb_val, reinterpret_cast<int *>(&b)+2, sizeof(int));  
    46.      cout<<*pa_val<<endl;59     cout<<*pb_val<<endl;  
    47.      memcpy(pb_val, reinterpret_cast<int *>(&b)+3, sizeof(int));  
    48.      //存在内存对齐的问题  
    49.      memcpy(pa_val, reinterpret_cast<int *>(&b)+4, sizeof(char));  
    50.      cout<<*pb_val<<endl;66     cout<<*pa_val<<endl;  
    51.      /* 
    52.      cout<<"<<<<<<<<<<<<<<"<<endl; 
    53.      *pa_val = 'B'; 
    54.      *pb_val = 999; 
    55.      memcpy(reinterpret_cast<int *>(&b)+1, pa_val, sizeof(char)); 
    56.      memcpy(reinterpret_cast<int *>(&b)+2, pb_val, 4); 
    57.      cout<<b.geta_Val()<<endl; 
    58.      cout<<b.getb_Val()<<endl; 
    59.      */      
    60.      return 0;  
    61. }  

http://blog.csdn.net/w616589292/article/details/50897070

原文地址:https://www.cnblogs.com/findumars/p/7465552.html