Is there a way to remove a key from a C++ map without deleting the contents?

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?

Answers


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 Shapes. 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;  
}

Need Your Help

How to fix The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

c# asp.net-mvc-4 datetime

I am trying to insert data from another database and this code was working until today. Now it doesn't anymore.

Why is ther white space after my inline-block?

html css layout css-float whitespace

I'm using the float style to remove white spaces and I want to center it, but another white space appears after my div#bar

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.