Hello . Suppose I have two containers , holding pointers to objects ,which share some of their elements . from http://www.cplusplus.com/reference/stl/list/erase/ it says that :
This effectively reduces the list size by the number of elements removed, calling each element's destructor before.
How can I remove an object from both containers without calling the destructor twice:
example
#include <map>
#include <string>
using namespace std;
//to lazy to write a class
struct myObj{
string pkid;
string data;
};
map<string,*myObj> container1;
map<string,*myObj> container2;
int main()
{
myObj * object = new myObj();
object->pkid="12345";
object->data="someData";
container1.insert(object->pkid,object);
container2.insert(object->pkid,object);
//removing object from container1
container1.erase(object->pkid);
//object descructor been called and container2 now hold invalid pointer
//this will call try to deallocate an deallocated memory
container2.erase(object->pkid);
}
please advice