#include <iostream>
#include <memory>
struct base { } ;
struct derived : base { ~derived( ) { std:: cout << "derived::~derived\n " ; } } ;
int main( )
{
{
std:: shared_ptr < base> p1( new derived ) ;
std:: shared_ptr < derived> p2( new derived ) ;
std:: shared_ptr < base> p3 = p2 ;
std:: shared_ptr < base> p4 = std:: make_shared < derived> ( ) ;
}
std:: cout << "the three derived class objects should have been destroyed by now\n " ;
{
std:: cout << "without a virtual destructor for base, there is trouble here\n " ;
std:: shared_ptr < base> p( static_cast < base* > ( new derived ) ) ;
}
std:: cout << "the wrong destructor would have been called for the derived class object\n " ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKc3RydWN0IGJhc2Uge307CgpzdHJ1Y3QgZGVyaXZlZCA6IGJhc2UgeyB+ZGVyaXZlZCgpIHsgc3RkOjpjb3V0IDw8ICJkZXJpdmVkOjp+ZGVyaXZlZFxuIiA7IH0gfTsKCmludCBtYWluKCkKewogICAgewogICAgICAgIHN0ZDo6c2hhcmVkX3B0cjxiYXNlPiBwMSggbmV3IGRlcml2ZWQgKSA7CiAgICAgICAgc3RkOjpzaGFyZWRfcHRyPGRlcml2ZWQ+IHAyKCBuZXcgZGVyaXZlZCApIDsKICAgICAgICBzdGQ6OnNoYXJlZF9wdHI8YmFzZT4gcDMgPSBwMiA7CiAgICAgICAgc3RkOjpzaGFyZWRfcHRyPGJhc2U+IHA0ID0gc3RkOjptYWtlX3NoYXJlZDxkZXJpdmVkPigpIDsKICAgIH0KICAgIHN0ZDo6Y291dCA8PCAidGhlIHRocmVlIGRlcml2ZWQgY2xhc3Mgb2JqZWN0cyBzaG91bGQgaGF2ZSBiZWVuIGRlc3Ryb3llZCBieSBub3dcbiIgOwoKICAgIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgIndpdGhvdXQgYSB2aXJ0dWFsIGRlc3RydWN0b3IgZm9yIGJhc2UsIHRoZXJlIGlzIHRyb3VibGUgaGVyZVxuIiA7CiAgICAgICAgc3RkOjpzaGFyZWRfcHRyPGJhc2U+IHAoIHN0YXRpY19jYXN0PGJhc2UqPiggbmV3IGRlcml2ZWQgKSApIDsKICAgIH0KICAgIHN0ZDo6Y291dCA8PCAidGhlIHdyb25nIGRlc3RydWN0b3Igd291bGQgaGF2ZSBiZWVuIGNhbGxlZCBmb3IgdGhlIGRlcml2ZWQgY2xhc3Mgb2JqZWN0XG4iIDsKfQo=