64,663
社区成员
发帖
与我相关
我的任务
分享
class Base1
{
public:
virtual void x() { cout << "Base1::x()" << endl; }
virtual void y() { cout << "Base1::y()" << endl; }
virtual void z() { cout << "Base1::z()" << endl; }
};
class Base2
{
public:
virtual void x() { cout << "Base2::x()" << endl; }
virtual void y() { cout << "Base2::y()" << endl; }
virtual void z() { cout << "Base2::z()" << endl; }
};
class Derive : public Base1, public Base2
{
public:
virtual void x1() { cout << "Derive::x1()" << endl; }
virtual void y1() { cout << "Derive::y1()" << endl; }
};
typedef void(*pFun)(void);
int main()
{
Derive b;
int** vptr = (int**)&b; // 虚函数表地址
// virtual table 1
pFun table1_func1 = (pFun)*((int*)*vptr+0); // vptr[0][0]
pFun table1_func2 = (pFun)*((int*)*vptr+1); // vptr[0][1]
pFun table1_func3 = (pFun)*((int*)*vptr+2); // vptr[0][2]
pFun table1_func4 = (pFun)*((int*)*vptr+3); // vptr[0][3]
pFun table1_func5 = (pFun)*((int*)*vptr+4); // vptr[0][4]
// virtual table 2
pFun table2_func1 = (pFun)*((int*)*(vptr+1)+0); // vptr[1][0]
pFun table2_func2 = (pFun)*((int*)*(vptr+1)+1); // vptr[1][1]
pFun table2_func3 = (pFun)*((int*)*(vptr+1)+2); // vptr[1][2]
// call
table1_func1();
table1_func2();
table1_func3();
table1_func4();
table1_func5();
table2_func1();
table2_func2();
table2_func3();
return 0;
}
// virtual table 1
pFun table1_func1 = (pFun)*((int*)*vptr+0); // vptr[0][0]
pFun table1_func2 = (pFun)*((int*)*vptr+2); // vptr[0][2]
pFun table1_func3 = (pFun)*((int*)*vptr+4); // vptr[0][4]
pFun table1_func4 = (pFun)*((int*)*vptr+6); // vptr[0][6]
pFun table1_func5 = (pFun)*((int*)*vptr+8); // vptr[0][8]
// virtual table 2
pFun table2_func1 = (pFun)*((int*)*(vptr+1)+0); // vptr[1][0]
pFun table2_func2 = (pFun)*((int*)*(vptr+1)+2); // vptr[1][2]
pFun table2_func3 = (pFun)*((int*)*(vptr+1)+4); // vptr[1][4]
Base1::x()
Base1::y()
Base1::z()
Derive::x1()
Derive::y1()
Base2::x()
Base2::y()
Base2::z()