今天發現dynamic_cast能夠不止爲具備繼續關係的類cast,還能夠這樣用:
// TestDynamic.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
class ICanFly
{
public:
virtual ~ICanFly(){}
virtual void Fly() = 0;
};
class ICanSwim
{
public:
virtual ~ICanSwim(){}
virtual void Swim() = 0;
};
class SuperMan: public ICanFly, public ICanSwim
{
public:
virtual void Fly()
{
std::cout << "Fly" << std::endl;
}
virtual void Swim()
{
std::cout << "Swim" << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ICanFly* pICanFly = new SuperMan();
ICanSwim* pICanSwim = dynamic_cast<ICanSwim*>(pICanFly);
pICanFly->Fly();
pICanSwim->Swim();
delete pICanFly;
return 0; }