#include <iostream>
#include<string>
using namespace std;
#include <stdio.h>
#include <stdarg.h>
class IBaseA
{
public:
virtual void fnA() = 0;
int m_nTestA;
};
class IBaseB
{
public:
virtual void fnB() = 0;
int m_nTestB;
};
class CTest : public IBaseA,public IBaseB
{
public:
virtual void fnA(){ printf("fnA\n"); }
virtual void fnB(){ printf("fnB\n"); }
};
int main(int argc, char* argv[])
{
CTest *pTest = new CTest;
void *p = (void*)pTest;
IBaseA *pBaseA = (IBaseA*)p; //错误,行为符合预期,reason:IBaseA是first base class
pBaseA->fnA();
IBaseB *pBaseB = (IBaseB*)p; //错误 ,行为不符合预期,指针会按照偏移强转
pBaseB->fnB();
pBaseB = (IBaseB*)pTest; //正确
pBaseB->fnB();
pBaseB = (IBaseB*)(CTest *)p; //正确
pBaseB->fnB();
getchar();
return 0;
}
g