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 回答