【问题标题】:ptr_map and pointerptr_map 和指针
【发布时间】:2010-06-27 18:08:11
【问题描述】:

我正在使用 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[] 返回引用!所以在constructur中可能有从值调用类型。 现在编译器失败并出现错误:

 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 中有任何方法返回指向该值的指针,则不会有任何问题。对此你有什么想说的?

【问题讨论】:

    标签: boost reference map pointers abstract


    【解决方案1】:

    一个经常被遗忘的事实是,如果键不存在,operator[] 将实例化该键。在您的情况下这是一个问题,因为密钥是抽象的。 因此,请改用 at()。 也就是说,

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

    有关更多信息,请阅读"Semantics: lookup" 部分

    顺便说一句,我会质疑您的设计决定,即公开存储在容器中的原始指针,其目的是减轻内存管理。

    【讨论】:

    • 可能你的意思是:operator[] 将实例化值/对象,而不是键。另外,这里的关键不是抽象的。
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2012-05-03
    相关资源
    最近更新 更多