まず、はいQList
、この目的で を使用できますが、最初にインターフェイス クラスを作成し、これを で使用することをお勧めします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;
また、必要に応じて、他のレスポンダーから示唆されているように、すべてを 1 つの構造体にカプセル化できます。
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;