#include <iostream>
using namespace std;
typedef void (__thiscall *Func)(void*pThis);
Func * GetVptr(void* pThis, int iOffset=0)
{
static union
{
int iVal;
Func * vptr;
};
iVal = *((int*)pThis+iOffset);
return vptr;
}
void PrintVptr(Func * vptr,void* pThis,int count = 3)
{
for (int i=0;i<count;++i)
{
cout<< "第"<<i+1<<"个虚函数指针地址:"<< vptr[i] <<" ";
vptr[i](pThis);
}
cout<<"---------------------------------------"<<endl;
}
class Base1{
public:
int i;
virtual void f(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void g(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void h(){cout <<__FUNCTION__<<" "<< i<<endl;}
};
class Base2{
public:
int i;
virtual void f(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void g(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void h(){cout <<__FUNCTION__<<" "<< i<<endl;}
};
class Base3{
public:
int i;
virtual void f(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void g(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void h(){cout <<__FUNCTION__<<" "<< i<<endl;}
};
class Derive : public Base1,public Base2 ,public Base3 {
public:
int i;
virtual void f(){cout <<__FUNCTION__<<" "<< i<<endl;}
virtual void g1(){cout <<__FUNCTION__<<" "<< i<<endl;}
};
int main(int argc, char* argv[])
{
Base1 b1;
b1.i=10;
Base2 b2;
b2.i=20;
Base3 b3;
b3.i=30;
Derive d;
d.i =60;
d.Base1::i=10;
d.Base2::i=20;
d.Base3::i=30;
Func * vptr = GetVptr(&b1);
PrintVptr(vptr,&b1);
vptr = GetVptr(&b2);
PrintVptr(vptr,&b2);
vptr = GetVptr(&b3);
PrintVptr(vptr,&b3);
vptr = GetVptr((Base1*)&d,0);
PrintVptr(vptr,&d);
vptr = GetVptr((Base2*)&d,0);
PrintVptr(vptr,(Base2*)&d);
vptr = GetVptr((Base3*)&d,0);
PrintVptr(vptr,(Base3*)&d);
return 0;
}