首先,是的,你可以使用 aQList
来达到这个目的,但是我建议先创建一个接口类并在你的QMap
.
struct GameObjectInterface {
};
class Safe : public GameObjectInterface {};
class Mushroom : public GameObjectInterface {};
class Floor : public GameObjectInterface {};
QMap<int, GameObjectInterface*> _GameObjects;
// Is game object with ID `n` a `Safe`?
Safe* s = dynamic_cast<Safe*>(_GameObjects[n]);
if (s != nullptr) {
// Yes it is a safe
}
另一种可能:
QList<QMap<int, GameObjectInterface*>> _GameObjects;
如果您愿意,您可以按照其他响应者的提示将所有内容封装到一个结构中。
struct MyGameObject {
QMap<int, Safe*> Safes;
QMap<int, Mushrooms*> Mushrooms;
QMap<int, Floor*> Floors;
};
QList<MyGameObject> _GameObjects;
如果每个都是相关的(所有对象的键相同),则可以简化为:
struct MyGameObject {
Safe* _Safe;
Mushrooms* _Mushroom;
Floor* _Floor;
};
QMap<int, MyGameObject*> _GameObjects;