C++中多继承和类型转换
#include "stdafx.h"
struct IX
{
virtual void FX() = 0;
};
struct IY
{
virtual void FY() = 0;
};
class CA:
public IX,
public IY
{
virtual void FX()
{
printf("CA->FX()\n");
}
virtual void FY()
{
printf("CA->FY()\n");
}
};
class CB:
public CA
{
virtual void FX()
{
printf("CB->FX()\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CA * pA = new CA;
CB * pB = new CB;
//多重继承和类型转换效果比较
printf("address of CA: %x\n", pA);
IX * px = pA;
IY *py = pA;
printf("IX address of CA: %x\n", px);
printf("IY address of CA: %x\n", py);
px->FX();
px = pB;
px ->FX();
py->FY();
py = pB;
py ->FY();
return 0;
}
struct IX
{
virtual void FX() = 0;
};
struct IY
{
virtual void FY() = 0;
};
class CA:
public IX,
public IY
{
virtual void FX()
{
printf("CA->FX()\n");
}
virtual void FY()
{
printf("CA->FY()\n");
}
};
class CB:
public CA
{
virtual void FX()
{
printf("CB->FX()\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CA * pA = new CA;
CB * pB = new CB;
//多重继承和类型转换效果比较
printf("address of CA: %x\n", pA);
IX * px = pA;
IY *py = pA;
printf("IX address of CA: %x\n", px);
printf("IY address of CA: %x\n", py);
px->FX();
px = pB;
px ->FX();
py->FY();
py = pB;
py ->FY();
return 0;
}
运行结果: