Does anyone know why the STL containers don\'t have virtual destructors?
As far as I can tell, the only benefits are:
Another solution to be able to subclass from STL containers is one given by Bo Qian using smart pointers.
Advanced C++: Virtual Destructor and Smart Destructor
class Dog {
public:
~Dog() {cout << "Dog is destroyed"; }
};
class Yellowdog : public Dog {
public:
~Yellowdog() {cout << "Yellow dog destroyed." << endl; }
};
class DogFactory {
public:
static shared_ptr createYellowDog() {
return shared_ptr(new Yellowdog());
}
};
int main() {
shared_ptr pd = DogFactory::createYellowDog();
return 0;
}
This avoids the dillema with virtual destructors altogether.