Reputation: 17223
Suppose I have a list as such
std::list<foo*> lst;
If I do the following:
lst.clear
will that call the destructor of all items in the list ? or will I have to call the destructor individually for each item ? If not what would be the best way to empty the contents of the list and have the destructor of the pointers called ? Will the following work or is there a better way to do it
for(
std::list<foo*>::iterator listMyClassIter = lst.begin();
listMyClassIter != lst.end();
listMyClassIter ++) {
delete listMyClassIter; }
lst.clear(); //Now clear the content
Upvotes: 0
Views: 81
Reputation: 10733
Your list is of pointers and pointers don't have destructors. That means you have to explicitly delete pointers in the container.
EDITED AS QUESTION IS EDITED:-
If you want your list to automatically manage memory for you then you can use boost::shared_ptr rather than storing raw pointers.
You can do either of following on raw pointers:-
for (...) { delete *it; } m_list.clear ();
OR
for (...) { delete *it; m_ilist.erase (); }
Upvotes: 3