很奇怪,運行失敗卻代碼成功
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
/**
?* 定義移動類:Movable
?* 純虛函數:move
?*/
class Movable
{
public:
? ? virtual void move() = 0;
};
/**
?* 定義公交車類:Bus
?* 公有繼承移動類
?* 特有方法carry
?*/
class Bus : public Movable
{
public:
? ? virtual void move()
? ? {
? ? ? ? cout << "Bus -- move" << endl;
? ? }
? ??
? ? void carry()
? ? {
? ? ? ? cout << "Bus -- carry" << endl;
? ? }
};
/**
?* 定義坦克類:Tank
?* 公有繼承移動類
?* 特有方法fire
?*/
class Tank : public Movable
{
public:
? ? virtual void move()
? ? {
? ? ? ? cout << "Tank -- move" << endl;
? ? }
? ? void fire()
? ? {
? ? ? ? cout << "Tank -- fire" << endl;
? ? }
};
/**
?* 定義函數doSomething含參數
?* 使用dynamic_cast轉換類型
?*/
void doSomething(Movable *obj)
{
? ? obj->move();
? ? if(typeid(*obj) == typeid(Bus))??
? ? {
? ? ? ? Bus *bus = dynamic_cast<Bus *>(obj);
? ? ? ? bus->carry();
? ? }
? ? if(typeid(*obj) == typeid(Tank))
? ? {
? ? ? ? Tank *tank = dynamic_cast<Tank *>(obj);
? ? ? ? tank->fire();
? ? }
}
int main(void)
{
? ? Bus b;
? ? Tank t;
? ? doSomething(&b);
? ? doSomething(&t);
? ? return 0;
}
2019-08-19
剛看了下我的vs里面外部依賴庫自帶typeinfo類
2019-08-19
我再visual studio中運行不用引用任何東西都可以,可能是編輯器運行時自動調用了什么類,而慕課網里面的網頁編輯器必須引用type_info類或者type_index類才可以運行成功。
2018-12-26
知道了,要加上頭文件 #include<typeinfo>
2018-12-26
怎么解決的
2018-12-02
已解決問題
2018-12-02
顯示的是