class MyMap : std::map<char, pro::image>
{
public:
void MyMethod(char x);
/** code **/
}
void MyMap::MyMethod(char x)
{
pro::image my_img; // note that my_img is a local variable
my_img.LoadFromFile("my_image.png");
this->insert(std::pair<char, pro::image>(x, my_img)); // stored in the class
}
现在,这段代码安全吗?基本上,当我插入
时,MyMap
是否存储my_img
的拷贝,还是存储my_img
的拷贝引用?
最佳答案
它将存储一个拷贝。
但是,你真的需要继承吗?您应该使 std::map
成为类成员。
class MyMap
{
std::map<car, pro::image> map_;
public:
void MyMethod(char x);
/** code **/
};
void MyMap::MyMethod(char x)
{
pro::image my_img; // note that my_img is a local variable
my_img.LoadFromFile("my_image.png");
map_.insert(std::pair<char, pro::image>(x, my_img)); // stored in the class
}
关于c++ - 将局部变量存储在类的 STL 容器中安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380460/