I have a Class named Shape, and a ShapeStorage Class. ShapeStorage class has a map…
std::map<int, Shape*> shapes;
and a function…
Shape * ReturnShapePointer(int key)
{
Shape* shape = shapes[key];
shapes.erase(key);
return shape;
}
My goal is to be able to have my main class instantiate a ShapeStorage object, store some Shape* in the shapes map. Then later on I want to delete it from my map, but not delete the value itself. I want my main class to still be able to access the value.
I have tried making it, and my pointer still returns correct values, but I’m afraid that since the destructor is being called for Shape when I delete the pointer from my map, so it’s just garbage data at that point.
Is there any way around this?
If you’re storing pointers,
mapwill not call your destructor. If it’s getting called, it’s getting called somewhere else.Try running this example:
You should get this: