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