#include "iostream"
using namespace std;
class Base1{
public:
virtual void f(){cout <<"Base1::f" <<endl;}
virtual void g(){cout <<"Base1::g" <<endl;}
virtual void h(){cout <<"Base1::h" <<endl;}
};
class Base2{
public:
virtual void f(){cout <<"Base2::f" <<endl;}
virtual void g(){cout <<"Base2::g" <<endl;}
virtual void h(){cout <<"Base2::h" <<endl;}
};
class Base3{
public:
virtual void f(){cout <<"Base3::f" <<endl;}
virtual void g(){cout <<"Base3::g" <<endl;}
virtual void h(){cout <<"Base3::h" <<endl;}
};
class Derive : public Base1, public Base2, public Base3{
public:
virtual void f(){cout <<"Derive::f" <<endl;}
virtual void g1(){cout <<"Derive::g1" <<endl;}
virtual void h1(){cout <<"Derive::h1" <<endl;}
};
typedef void(*Fun)(void);
int main()
{
Fun pFun = NULL;
Derive d;
int** pVtab = (int**)&d;
cout <<"虚函数表地址:" <<(int*)&d <<endl;
/* Base1's vtable */
//pFun = (Fun)*((int*)*((int*)&d + 0) + 0);
pFun = (Fun)pVtab[0][0];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 0) + 1);
pFun = (Fun)pVtab[0][1];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 0) + 2);
pFun = (Fun)pVtab[0][2];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 0) + 3);
pFun = (Fun)pVtab[0][3];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 0) + 4);
pFun = (Fun)pVtab[0][4];
pFun();
/* Base2's vtable */
//pFun = (Fun)*((int*)*((int*)&d + 1) + 0);
pFun = (Fun)pVtab[1][0];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 1) + 1);
pFun = (Fun)pVtab[1][1];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 1) + 2);
pFun = (Fun)pVtab[1][2];
pFun();
/* Base3's vtable */
//pFun = (Fun)*((int*)*((int*)&d + 2) + 0);
pFun = (Fun)pVtab[2][0];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 2) + 1);
pFun = (Fun)pVtab[2][1];
pFun();
//pFun = (Fun)*((int*)*((int*)&d + 2) + 2);
pFun = (Fun)pVtab[2][2];
pFun();
return 0;
}
C++虚函数表理解
最新推荐文章于 2023-04-07 15:54:33 发布