3

基本抽象型から派生したオブジェクトを格納するために、boostのptr_mapを使用しています。

class Entity { virtual void foo() = 0; };
class Entity1 : public Entity {};
class Entity2 : public Entity {};

boost::ptr_map<string, Entity> someMap; // We could store pointers for abstract type

挿入はうまく機能します:

someMap.insert("someKey", new Entity1());
someMap.insert("someKey", new Entity2());

しかし、マップから戻っていない:

template<typename EntityType>
EntityType *GetEntity(const string &entityName)
{
    return dynamic_cast<EntityType*>(&someMap[entityName]);
}

GetEntity<Entity1>(entityName);

ここで問題:ptr_mapのoperator []が参照を返します!したがって、コンストラクターでは、値から型を呼び出すことができます。コンパイラはエラーで失敗します:

 instantiated from ‘EntityType* EntityManager::GetEntity(const std::string&) [with EntityType = Entity1, std::string = std::basic_string<char>]’
error: cannot allocate an object of abstract type ‘Entity’

値へのポインタを返すメソッドがptr_mapにある場合、問題はありません。これについて何と言えますか?

4

1 に答える 1

4

忘れられがちな事実は、キーが存在しない場合、operator[]がキーをインスタンス化することです。キーが抽象的であるため、これはあなたの場合の問題です。したがって、代わりにat()を使用してください。あれは、

return dynamic_cast<EntityType*>(&someMap.at(entityName));

詳細については、「セマンティクス:ルックアップ」セクションをお読みください。

ところで、メモリ管理を軽減することを目的とするコンテナ内に格納されている生のポインタを公開するという設計上の決定に疑問を投げかけます。

于 2010-06-27T22:55:11.143 に答える