关于c++中运行时类型识别的问题

kodobeast kodobeast 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写这段的代码的目的是,我想建立个链表。存储所有的对象的指针,我想把他们都转化成动物基类的指针,用的时候在转换回来。
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++支持吗?如果可以的话,怎么改这段代码
...全文
评论 19
收藏 已收藏
分享
返回顶部
kodobeast
C++ 语言 65,212 成员 250,513 内容