std::list (or any of the standard containers) doesn't have a virtual destructor... so you can't really do it. I was suprised g++ even let me compile this without warning (despite deriving from class with non-virtual destructor)
Code:
#include <list>
#include <iostream>
class check {
public:
check() { count++;}
~check() {count--;}
static int getCount() { return count; }
private:
static int count;
};
int check::count=0;
template <class T> class ptrList: public std::list<T*> {
public:
ptrList() { }
~ptrList() {
for (std::list<T*>::iterator i = begin(); i != end(); i++) {
delete *i;
}
}
};
int main() {
std::list<check*>* blah = new ptrList<check>;
blah->push_back(new check);
blah->push_back(new check);
delete blah; // doesn't call ~ptrList() because of non-vrituality of ~std::list()
std::cout << check::getCount() << std::endl; // would be 0 if they were deleted..
return 0;
}