Look at this code:
class test
{
public:
test() { cout << "Constructor" << endl; };
virtual ~test() { cout << "Destructor" << endl; };
};
int main(int argc, char* argv[])
{
test* t = new test();
delete(t);
list<test*> l;
l.push_back(DNEW test());
cout << l.size() << endl;
l.clear();
cout << l.size() << endl;
}
And then, look at this output:
Constructor
Destructor
Contructor
1
0
The question is: Why is the destructor of the list element not called at l.clear()?
Your list is of pointers. Pointers don’t have destructors. If you want the destructor to be called you should try
list<test>instead.