destruct std::map that contains member created on heap

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

  1. princemaozh

    princemaozh New Member

    Aug 28, 2007
    Likes Received:
    Trophy Points:
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice