1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

destruct std::map that contains member created on heap

Discussion in 'C' started by princemaozh, May 19, 2008.

  1. princemaozh

    princemaozh New Member

    typedef std::vecotr<MyClass*> MyVec;
    typedef std::map<string, MyVec> MyMap;

    MyVec* myVec1 = new MyVec();
    myVec1->push_back(new MyClass("1_1"));
    myVec1->push_back(new MyClass("1_2"));

    MyVec* myVec2 = new MyVec();
    myVec2->push_back(new MyClass("2_1"));
    myVec2->push_back(new MyClass("2_2"));

    MyMap* myMap = new MyMap();
    myMap->insert(pair<string, MyVec>("name1", *myVec1));
    myMap->insert(pair<string, MyVec>("name2", *myVec2));

    question 1)
    How to delete the myMap without any memory leak? Seem there is no direct way, we need to iterate over the map and vector, and do "delete" level by level, right?
    Any better way? (Source code is prefered)

    quesiont 2)
    MyMap aMap;
    aMap = *myMap;
    This assignment ("=") operation is deep copy or shallow copy?

    YOur answer is appreciated. Thanks.

Share This Page