Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-05-05 13:27:25

----- Mensaje original -----
De: Jeff Garland <jeff_at_[hidden]>
Fecha: Sábado, Mayo 5, 2007 6:10 pm
Asunto: Re: [boost] [1.34.0beta] many, many warnings... :(
Para: boost_at_[hidden]

> Jonathan Franklin wrote:
> > Interesting. I'm obviously flaunting my ignorance, but I didn't
> > realize that inheriting from a class sans virtual dtor was ever
> > a Good Thing. I'll have to read up on the issues WRT
> is_polymorphic.
> I find inheriting without virtual functions quite useful. The
> main reason for the virtual destructor rule goes back to Scott
> Meyers Effective C++. He points out that if you have
> Base <-- Derived and you call delete Base* the destructor of
> Derived is not called. In general, it's hard to ensure your
> derived classes won't need the destructor called and hence the
> rule.
> However, if Derived happens to have a trivial/empty destructor
> then, really there's no reason to call it.

I agree and disagree with your previous exposition :)

I think deriving without virtual constructors is fine in those
contexts when the user is not expected to delete passed
objects, which are by far the most usual. Almost every
function of the form

  void foo([const] T& t)

won't certainly try to delete t, so it's fine to pass an
use here an object of some class derived from T (like in
your super_string case.)

IMHO the contexts in which objects are passed around
without ownership transfer are overwhelmingly more numerous
than those where transfer is done, so I think this "don't
derive without virtual dtor" is somewhat overrated. There's
the special case when the base is abstract or has some
virtual function: here it is more likely that the class was
designed with ownership transer via pointers in mind, and
the rule would be more sensible. But then again, a great
many classes don't have any virtual member function,
much more so in the template-dominated C++ of our days.

What I don't agree with you in is your claim that
"if Derived happens to have a trivial/empty destructor
then, really there's no reason to call it." If my
memory serves me well, deleting a Derived object through
its Base non-virtual dtor is illegal according to the
standard, regardless of whether D's dtor is trivial or
not. You're merely being lucky with the actual behavior
exhibited by compilers.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Boost list run by bdawes at, gregod at, cpdaniel at, john at