App*_*ker 2 c++ variables containers stl map
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
}
Run Code Online (Sandbox Code Playgroud)
现在,这段代码安全吗?基本上,做MyMap存储拷贝的my_img时候insert,或者它存储的参考?
它会存储一份副本.
但是,你真的需要继承吗?你应该成为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
}
Run Code Online (Sandbox Code Playgroud)