C++单继承、多继承情况下的虚函数表分析

  • 2019 年 10 月 3 日
  • 笔记

C++??????????????????????????????????????????????VTAB???????????????????VTAB??????VPTR??????????????????????????????????VPTR??VTAB???????????????????

 

?????1.http://blog.lucode.net/programming-language/cpp-vtab-and-call-convention.html

??2.https://blog.csdn.net/tangaowen/article/details/5830803

??3.?????C++?????

?????

 1 #include<iostream>   2 #include <stdio.h>   3 using namespace std;   4 class A {   5 public:   6     void func() {   7         cout << "A::func()" << endl;   8     }   9     virtual void func1() {  10         cout << "A::func1(): " << endl;  11     }  12  13     virtual void func3() {  14         cout << "A::func3(): " << endl;  15     }  16 };  17  18 class B: public A {  19 public:  20     virtual void func() {  21         cout << "B::func()" << endl;  22     }  23     virtual void vfunc() {  24         cout << "B::vfunc()" << endl;  25     }  26     void func1() {  27         cout << "B::func1(): " << endl;  28     }  29 };  30 int main() {  31     typedef void (*Fun)(void);  32     B a;  33  34     Fun *fun = NULL;  35     fun = (Fun*) ((int *) *(int *) &a);  36 //    fun = *(Fun **) &a;  37     fun[0]();  38     fun[1]();  39     fun[2]();  40     fun[3]();  41  42     return 0;  43 }

?????

B::func1():
A::func3():
B::func()
B::vfunc()

??????

 1 #include<iostream>   2 #include <stdio.h>   3 using namespace std;   4 class B1 {   5 public:   6     virtual void barB1() {cout << "B1::bar" << endl;}   7     virtual void fooB1() {cout << "B1::foo" << endl;}   8 };   9  10 class B2 {  11 public:  12     virtual void barB2() {cout << "B2::bar" << endl;}  13     virtual void fooB2() {cout << "B2::foo" << endl;}  14 };  15  16 class D : public B1, B2 {  17 public:  18     void fooB1() {cout << "D::foo" << endl;}  19     void barB2() {cout << "D::bar" << endl;}  20 };  21  22 typedef void (*Func)();  23 int main() {  24     D tt;  25     Func* vptr1 = *(Func**)&tt;  26     Func* vptr2 = *((Func**)&tt + 1);  27  28     vptr1[0]();  29     vptr1[1]();  30     vptr1[2]();  31     cout<<"\\\"<<endl;  32     vptr2[0]();  33     vptr2[1]();  34  35     return 0;  36 }

?????

B1::bar
D::foo
D::bar
\
D::bar
B2::foo

???

     ???????????????????????????????????????????????????????????????????????????????????????????????????????????????

  

??????? ?? ?? ?? ????

?? ?C++????????????????????????????????

?? ?C++???????????????????virtual???????????????virtual????????????????????????????????????????????

?? ??????????????????????

 

 

// ????????????+????????????https://cloud.tencent.com/developer/support-plan?invite_code=3ph7kzdx2saoo