在c++中为了支持面向对象多态特性,采用了函数虚表(virtual function table)来实现。用以下的例子来分析虚函数表的具体实现方法:
#include <iostream>
using namespace std;
class CBase
{
public:
virtual void fun1()
{
cout<<"fun1 in CBase"<<endl;
}
virtual void fun2()
{
cout<<"fun2 in CBase"<<endl;
}
virtual ~CBase(){};//虚函数地址在虚表中的位置和声明一致(VS2003)
};
class CBase1
{
public :
virtual ~CBase1(){};//
virtual void fun3()
{
cout<<"fun3 in CBase1"<<endl;
}
};
//继承了两个基类,这将使编译器在CDerive类中生成2个虚表,每个基类都会有自己的虚函数表
class CDerive:public CBase,public CBase1
{
public:
CDerive()
{
}
void fun1()
{
cout<<"fun1 in CDerive"<<endl;
}
};
typedef void(*Func)(void);
void callFunctionAtAddress(int nAddress)
{
printf("Call function at address:0x%x/r/n",nAddress);
Func fun = (Func)nAddress;
fun();
}
int main(int argc, char argv[])
{
CDerive *pObj = new CDerive;
//new 出来的CDerive指针实际上是虚函数表的指针,指向的是第一个父类的虚表指针
int vtbAddress = *(int *)pObj;//32位系统中,int类型宽度为指令宽度:4BYTE
printf("virtual function table address:0x%x/r/n",vtbAddress);
//取得第一个父类(CBase)中第一个虚函数的地址
int funAddress = *(int *)vtbAddress;
callFunctionAtAddress(funAddress);
//取得第一个父类(CBase)中第二个虚函数的地址
//注意第三个虚函数地址将是父类CBase虚构函数地址,直接调用将会引起出错。
funAddress = *((int *)vtbAddress+1);
callFunctionAtAddress(funAddress);
//取得第二个父类(CBase1)的虚表指针
vtbAddress = *((int *)pObj + 1);
funAddress = *(int *)vtbAddress;
callFunctionAtAddress(funAddress);
delete pObj;
return 0;
}
在xp,vs2003编译通过,运行的结果为:
virtual function table address:0x452164
Call function at address:0x41b063
fun1 in CDerive
Call function at address:0x41a384
fun2 in CBase
Call function at address:0x41b194
fun3 in CBase1
C++虚表
最新推荐文章于 2025-03-02 00:53:56 发布