Sólo para añadir un ejemplo más de Daniel Lidström's answer
As long as you always store your allocated objects in a shared_ptr, then you really don't need a virtual destructor.
Si se utiliza un shared_ptr así:
std::shared_ptr<Base> b(new Concrete);
A continuación, se invoca el destructor de hormigón y el destructor de base para destruir el objeto.
Si se utiliza un shared_ptr así:
Base* pBase = new Concrete;
std::shared_ptr<Base> b(pBase);
Sólo entonces el destructor base se denomina en la destrucción del objeto.
Esto es an example
#include <iostream> // cout, endl
#include <memory> // shared_ptr
#include <string> // string
struct Base
{
virtual std::string GetName() const = 0;
~Base() { std::cout << "~Base\n"; }
};
struct Concrete : public Base
{
std::string GetName() const
{
return "Concrete";
}
~Concrete() { std::cout << "~Concrete\n"; }
};
int main()
{
{
std::cout << "test 1\n";
std::shared_ptr<Base> b(new Concrete);
std::cout << b->GetName() << std::endl;
}
{
std::cout << "test 2\n";
Base* pBase = new Concrete;
std::shared_ptr<Base> b(pBase);
std::cout << b->GetName() << std::endl;
}
}
posible duplicado de [Destructors predeterminados virtuales en C++] (http://stackoverflow.com/questions/827196/virtual-default-destructors-in-c) –