Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   destruct std::map that contains member created on heap (http://www.go4expert.com/forums/destruct-stdmap-contains-created-heap-t10667/)

princemaozh 19May2008 21:14

destruct std::map that contains member created on heap
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.

All times are GMT +5.5. The time now is 02:32.