如何从给定类型C ++的向量返回元素? [英] How to return elements from a vector with a given type C++?
本文介绍了如何从给定类型C ++的向量返回元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i have a class EscapeRoomWrapper now i made another two classes which are the "sons" of this class ScaryRoom and KidsRoom , which means ScaryRoom is a EscapeRoomWrapper and KidsRoom is a EscapeRoomWrapper , now for each one of those classes i wrote functions ..
i am working now from another class which is company , and in this class i added a set of all the rooms (EscapeRoomWrapper,ScaryRoom,KidsRoom) now i want to write a function in class company that i give it a type of room and it should returns for me all of the rooms from that type that exists in the victor of all rooms , then i thougt about using typeid but then the professer banned us from using it , my last idea is to use cast dynamic
<pre>any ideas of how i can do this function ?? <pre>std::vector<EscapeRoomWrapper*>& getAllRoomsByType(RoomType type) const
。
我尝试过:
public:
//这里的功能
};
private:
int a
public:
// y房间的功能
};
class:public {
.
What I have tried:
public:
// functions here
};
private:
int a
public:
// functions for y room
};
class : public {
推荐答案
如果EscapeRoomWrapper有一个RoomType成员,那么它知道每个派生对象的类型是什么,它可以使用那与dynamic_cast。如果每个派生的被调用方法相同,那么您将不需要进行强制转换。
If EscapeRoomWrapper has a RoomType member then it knows what type each derived object is and it can use that with dynamic_cast. If the called methods are the same for each derivation then you won't need to cast.
这篇关于如何从给定类型C ++的向量返回元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文