virtual destructor unexpected pass

Alexandre Oliva oliva@dcc.unicamp.br
Wed Sep 2 00:00:00 GMT 1998


Todd Vierling <tv@pobox.com> writes:

> On 2 Sep 1998, Alexandre Oliva wrote:

> : Nope, it is valid to declare an pure virtual destructor, but it *must*
> : be defined anyway if you instantiate any subclass of the class.  A
> : compile-time error would be wrong, because the destructor could be
> : defined in another translation unit.

> If it's defined in another unit for the superclass, doesn't that mean it
> shouldn't have the "= 0" clause (pure virtual)?

No, even a member function that is declared pure virtual can be
given a definition, that will be used whenever a qualified name is
used to refer to it.  Since the destructor of a subclass explicitly
invokes the destructor of the superclass, a pure virtual destructor
must be defined if any subclass of its containing class is
destructed.

> The problem I describe in my case is that, in the case of the pure
> virtual destructor, that the compiler still puts in references to
> this destructor - which does not, nor will ever, exist!

The compiler can't tell, and it is required to generate that
reference [class.dtor]/7.

> If this is standards-conformant to have a pure virtual destructor, I then
> believe that the correct thing here is for the compiler to treat such a
> destructor as being a null clause, i.e. {}.  Otherwise, you can't use pure
> virtual destructors, because they won't link.

You can only use functions that are defined in some translation unit.
Treating pure virtual destructors as if they were to be implicitly
generated would make it impossible to provide a real definition for
it.  Pure virtual destructors are useful for making a class abstract
without having to define other abstract member functions.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil




More information about the Gcc-bugs mailing list