[comp.lang.c++] Pure virtual destructors

kvt@drutx.ATT.COM (TranKV) (02/07/90)

Commenting on PURE VIRTUAL DESTRUCTORS

In article <10422@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes:
....

>Therefore, it doesn't make sense to have a pure virtual
>destructor, as it would preclude destroying objects not
>only of that class but of any class derived from it.

I might be ignorant here but to counter Andrew's comments:

    1. "preclude destroying objects ... of that class"
    From C++ usage point of view, this is perfectly OK since the base
    class might just be a place holder (i.e. no data members declared in
    class definition; therefore nothing needs to be destroyed.) I 
    understand that in C implementation, every C++ class requires some 
    data; but as a C++ user, should I be required to be aware of its 
    C implementation?

    2. "... not only of that class but of any class derived from it."
    I don't understand this. As I understand C++, a VIRTUAL BASE
    destructor forces the VIRTUAL DERIVED class destructor to be called
    first (when the derived object is pointed at by a base class 
    pointer), and then other destructors in the class hierarchy. If 
    that's so, why a PURE virtual base class destructor would prevent 
    the VIRTUAL DERIVED class destructor to be called? I'd assume 
    everything works as expected except when it's the turn  of the 
    base class destructor when  nothing will be done. But that's OK
    (see my argument above.)

    I'm confused.

Besides, if pure virtual destructors don't really make sense, why Cfront
doesn't flag it as errors but let linker complain about it?

Kim Tran
Bell Labs
kvt@drutx.att.com

roger@decvax.UUCP (Roger H. Scott) (02/08/90)

In article <5033@drutx.ATT.COM> kvt@drutx.ATT.COM (TranKV) writes:
>Commenting on PURE VIRTUAL DESTRUCTORS
>...
>I might be ignorant here but to counter Andrew's comments:
I think the confusion here stems from Tran's misconception that a
pure virtual function "does nothing" when "called".  The only thing that
pure virtuals do when called is abort your program - if you want to do
"nothing" write "{}" as the body of your function [this may still do more
than "nothing" is the case of ctors and dtors].
It is slightly unfortunate, but less than earth-shattering, that in order
to provide a virtual "basis" for destructors in a class hierarchy you have
do declare and define a "pointless" destructor in a [quite possibly abstract]
base class.  This destructor does nothing useful and does require at least
a function call to execute [unless it is inline, but inline virtuals are a
BAD IDEA with AT&T's 2.0].

shopiro@alice.UUCP (Jonathan Shopiro) (02/11/90)

In article <7180012@hpfcbig.SDE.HP.COM>, jenings@hpfcbig.SDE.HP.COM (Byron T. Jenings Jr.) writes:
> I was trying to specify:
> 
> This is an abstract base class that cannot exist, and so has nothing
> but pure virtual functions.  Since it cannot exist, it has no
> destructor, so there's obviously no base class destructor for derived
> classes to call.
The summary says it all.  Objects of the virtual base class do exist,
as subobjects of instances of any derived class.  It would be easy to
check if a base class has a pure virtual destructor, and skip the
call, but it is often the case that the compiler has to generate some
code in the destructor and there would be no place to put it if the
destructor were pure virtual.  Probably the best thing would have been
to generate an error message immediately upon seeing a pure virtual
destructor instead of letting it get to a link failure.

So use an empty destructor.  It can be inline or not as you like.  The
situation with duplicated vtables is much improved in C++ 2.0 over 1.2
(and it will get beter in the future), so I would recommend trying it
both ways to see which you like better.
-- 
		Jonathan Shopiro
		AT&T Bell Laboratories, Warren, NJ  07060-0908
		research!shopiro   (201) 580-4229