取回后损坏的所以我已经编辑完成我的问题。 我有一个map
称为mTextMap
其中包含:对象在地图从地图
typedef std::map<const std::string, Text*> TextMap;
TextMap mTextMap;
而且我有以下几种方法:
void Foo::setUpGame()
{
Text text(1,2,3,4); //create a Text object
mTextMap["MainText"] = &text; //save it in the map!
}
Text& Foo::getText(const std::string name)
{
auto i= mTextMap.find(name);
return *(i->second); //Gets a reference to the Text that is inside the map
}
现在,如果我用这样的方式:
Foo foo;
foo.setUpGame();
Text& myText = foo.getText("MainText"); // Why is this corrupted?
对象myText
完全损坏!
这是怎么发生的?
使用['unique_prt'](http://www.cplusplus.com/reference/memory/unique_ptr/)或['shared_prt'](http://www.cplusplus.com/reference/memory/shared_ptr/ )作为地图中的值类型。如果你是自己管理记忆,那很容易犯错。 –
如果您愿意,我们检查了您的代码,提供[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –
你为什么要存储指向objs而不是obj本身的指针? – MikeMB