c++ std containers calling destructors

Started by
3 comments, last by ChaosEngine 14 years ago
I was reading the list reference on cplusplus.com and I found something: it says when I call clear or erase, for each element it removes it calls its destructor. so if I have

Object *o=new Object();
list<Object *> objects;
objects.insert(o);
objects.clear();
will o be deleted? or does it simply erase the pointer in the list? Because I don't want the list deleting my objects if it's not creating them too.
Advertisement
Standard library containers won't call delete on pointers they hold.
The destructor of the object stored in the vector will be called. Your vector stores pointers, and pointers don't have empty an destructor (or no destructor at all, if you want). It doesn't do anything with the object being pointed to, because the vector cannot know what has to be done.
Pointer variables don't have destructors. If you want that behavior, use a smart pointer like boost::shared_ptr.
Quote:Original post by DevFred
Pointer variables don't have destructors. If you want that behavior, use a smart pointer like boost::shared_ptr.


or a boost pointer container
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement