Boost logo

Boost :

From: Luigi Ballabio (ballabio_at_[hidden])
Date: 2002-01-14 12:24:16


At 08:56 AM 1/14/02 -0500, you wrote:
>struct Base { Base(); ~Base(); };
>struct Derived : Base { Derived(); ~Derived(); };
>
>void unsafe()
>{
> shared_ptr<Derived> d(new Derived);
> shared_ptr<Base> b(d);
> d.reset();
> b.reset(); // equivalent to delete (Base*)(new Derived)!!
>};
>
>Isn't shared_ptr just screaming for the application of John Maddock's new
>is_polymorphic<> trait?

Maybe I'm just about to embarass myself, but isn't Base just screaming for
having a virtual destructor instead, if you want to use it this way?

IMHO the point here is that a virtual or a non-virtual destructor are a
strong statement: a virtual destructor means "My derived classes are meant
to be used polymorphically: go ahead and upcast", while a non-virtual one
means "My derived classes are meant to be used as such: pray do not upcast."

If the user wants to upcast, there's nothing in the language to prevent
him. But if he does, then he's on his own since he disregarded an explicit
(well, IMHO anyway) hint by the class writer.

Bye,
         Luigi


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk