Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2007-05-05 14:10:12


JOAQUIN LOPEZ MU?Z wrote:

>> 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 :)

That's usual ;-)

> 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.)

Sure, but in the general case you could easily create an interface like this:

   void foo(T* t)

hence Scott's design rule.
>...snip...
>
> 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.

By the standard I believe it is technically undefined behavior. But as a
practical matter it's defined exactly the same on all compilers -- call the
base class destructor.

Jeff


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