关于c++中运行时类型识别的问题
我写这段的代码的目的是,我想建立个链表。存储所有的对象的指针,我想把他们都转化成动物基类的指针,用的时候在转换回来。
class animal
{
public:
animal(void);
~animal(void);
void virtual fly();
};
animal::animal(void)
{
}
animal::~animal(void)
{
}
void animal::fly()
{
}
class bird :public animal
{
public:
bird();
~bird();
void fly();
};
class dog: public animal
{
public:
dog();
~dog();
void brak();
};
bird::bird()
{}
bird::~bird()
{}
void bird::fly()
{
std::cout<<"fly"<<std::endl;
}
dog::dog()
{}
dog::~dog()
{}
void dog::brak()
{}
// testRTTI.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include ".\animal.h"
#include "test.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
bird birdObject;
dog dogObject;
animal* pointBird=&birdObject;
animal* pointDog=&dogObject;
if(typeid(*pointBird)==typeid(bird))
cout<<" find bird"<<endl;
else
cout<<"connot change "<<endl;
bird* pointB=dynamic_cast<bird*>(pointBird);
//如果这的fly不是虚函数行吗?
pointB->fly();
return 0;
}
这种运行时的转换为什么不能执行阿,c++支持吗?如果可以的话,怎么改这段代码