automatically delete remove item ptr from stl::list

If I have a very simple [CPerson] class and have a list of CPerson pointers like:

1
2
3
4
5
6
list<CPerson *>  lstPersonPtrs;

// Function that loads CPerson ptrs to list supplied
LoadPersonPtrs(lstPersonPtrs);

lstPersonPtrs.erase(lstPersonPtrs.begin(), lstPersonPtrs.end());


where I load the list [lstPersonPtrs] in function [LoadPersonPtrs] and then call erase on entire list.

I added code to my CPerson destructor to output the details of the CPerson object being destructed.

None of this output from the ~CPerson was fired. This would suggest that the CPerson pointers were never deleted.

My question is: do I have to manually delete each list item pointer or is there some automatic way of achieving this, such that the remove_if method also deletes a removed item ptr?

You are storing pointers so the pointer destructor will be called (it does nothing)
Either delete them manually, or use a smart pointer.
Thank you - I forgot about using a smart pointer.
Topic archived. No new replies allowed.