假设有基类A和B,派生类C公有继承于A和B:
Class C : public A , public B
现有如下代码:
class A
{
private:
int m_iA;
};
class B
{
private:
int m_iB;
};
class C : public A, public B
{
};
void fun(void* pVoid)
{
A* pA = (A*)pVoid;
B* pB = (B*)pVoid;
}
int _tmain(int argc, _TCHAR* argv[])
{
C* pObj = new C();
A* pASrc = pObj;
B* pBSrc = pObj;
fun((void*)pObj);
delete pObj;
pObj = NULL;
return 0;
}
运行一次各个指针值如下:
pObj 0x00399180
pASrc 0x00399180
pBSrc 0x00399184
pA 0x00399180
pB 0x00399180
pA 和 pB 的值一样了,如果用pB去掉该基类的方法就会悲剧的,直接找不到正确的方法入口地址。
如此看来,以后code时一定要注意了,不要随便用void*来传递对象的地址啊