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, map
will not call your destructor. If it's getting called, it's getting called somewhere else.
Try running this example:
#include <iostream>
#include <map>
class Shape {
public:
~Shape() {
std::cout << "Shape deleted" << std::endl;
}
};
int main(int argc, char *argv[]) {
std::map<int, Shape *> shapes;
shapes[1] = new Shape();
std::cout << "Shape added" << std::endl;
shapes.erase(1);
std::cout << "Shape removed from map, now exiting" << std::endl;
}
You should get this:
Shape added
Shape removed from map, now exiting
Your map only holds pointers to Shape
s. Removing a pointer from a map has no effect whatsoever on the object it points to, it only destroys the pointer in the map.
Also, your function will perform a lookup by key twice. Once when using operator []
and second time when calling erase ()
with a key. Here is a better way of doing this:
Shape *ReturnShapePointer (int key)
{
Shape *shape;
std::map<int, Shape*>::iterator it = shapes.find (key);
if (it != shapes.end ())
{
shape = it->second;
shapes.erase (it);
}
else
{
shape = NULL;
}
return shape;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With