typeid和继承 [英] typeid and inheritance
问题描述
这是我的课程。他们每个人都有虚拟析构函数:
class Object {...};
class Door:public Object {...};
class ClosedDoor:public Door {...};
class OpenedDoor:public Door {...};
and here''这些对象的向量:
vector< Object * objList;
objList.push_back(new ClosedDoor);
运算符typeid返回类ClosedDoor。但我想知道它是什么呢?
是一扇门。有没有办法找到这个?
感谢您的回复...
Here are my classes. Each of them has virtual destructor:
class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };
and here''s a vector of those objects:
vector<Object*objList;
objList.push_back( new ClosedDoor );
Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?
Thanks for replies...
推荐答案
ms****@gmail.com 写道:
这是我的课程。他们每个人都有虚拟析构函数:
class Object {...};
class Door:public Object {...};
class ClosedDoor:public Door {...};
class OpenedDoor:public Door {...};
and here''这些对象的向量:
vector< Object * objList;
objList.push_back(new ClosedDoor);
运算符typeid返回类ClosedDoor。但我想知道它是什么呢?
是一扇门。有没有办法找到这个?
Here are my classes. Each of them has virtual destructor:
class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };
and here''s a vector of those objects:
vector<Object*objList;
objList.push_back( new ClosedDoor );
Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?
最好的方法是在Object上有一个虚拟方法,告诉你
是什么。
秒(通过远景)是使用dynamic_cast。
The best way is to have a virtual method on Object that tells you what
it is.
Second (by a long shot) is to use dynamic_cast.
最好的方法是在Object上有一个虚拟方法,告诉你
The best way is to have a virtual method on Object that tells you what
它是。
第二个(通过远景)是使用dynamic_cast。
it is.
Second (by a long shot) is to use dynamic_cast.
但是仍然。我可以创建一个方法type_info GetTypeInfo(){return
typeid(* this); }。调用objList [0] - > GetTypeInfo(),它将返回
ClosedDoor。我希望它告诉我,ClosedDoor是一扇门...
But still. I can create a method type_info GetTypeInfo() { return
typeid( *this); }. Call objList[0]->GetTypeInfo() and it will return
ClosedDoor. I want it to tell me that ClosedDoor is a Door...
Gianni Mariani写道:
Gianni Mariani wrote:
< a href =mailto:ms **** @ gmail.com> ms **** @ gmail.com 写道:
>这是我的课程。他们每个人都有虚拟析构函数:
class Object {...};
类门:public Object {...};
类ClosedDoor:public Door {.. 。};
类OpenedDoor:public Door {...};
这里是这些对象的向量:
vector< Object * objList;
objList.push_back(new ClosedDoor);
运算符typeid返回类ClosedDoor。但我想知道它是一扇门。有没有办法找到这个?
>Here are my classes. Each of them has virtual destructor:
class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };
and here''s a vector of those objects:
vector<Object*objList;
objList.push_back( new ClosedDoor );
Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?
最好的方法是在Object上有一个虚拟方法,告诉你
是什么。
秒(通过远景)是使用dynamic_cast。
The best way is to have a virtual method on Object that tells you what
it is.
Second (by a long shot) is to use dynamic_cast.
为什么在每个
子类中添加虚拟成员函数并重载它比使用该语言的内置工具更好这份工作?
Why would adding a virtual member function and overloading it in every
subclass be better than using the language''s built-in tool for the job?
这篇关于typeid和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!