Topic: virtual destructors by default
Author: ark@alice.att.com (Andrew Koenig)
Date: 4 Jan 93 03:40:43 GMT Raw View
There are two problems with extending C++ so that
classes with virtual functions also automatically acquire virtual destructors:
1. It will be a long, long time before a careful programmer can take
advantage of the feature, because if a program that does so is
run under an implementation that doesn't support it, it will
quietly do the wrong thing and the user will blame the author.
In general, extensions that define previously undefined behavior
(rather than defining previously illegal behavior) must be viewed
with caution.
2. The proposal doesn't solve the whole problem: it is possible to write
class definitions that have no virtual functions and nevertheless
require a virtual destructor. For instance:
struct Thing { Thing(); ~Thing(); };
struct B { };
struct D: B { Thing t; };
main()
{
B* bp = new D;
delete bp; // Oops!
}
The commented line is undefined unless D has a virtual destructor.
For these reasons, I am dubious about the proposal. I do think, though,
that it is wise for compilers to issue warning messages for classes
with virtual functions that do not also have virtual destructors.
--
--Andrew Koenig
ark@europa.att.com
Author: harvey@opl.com (Harvey Reed)
Date: 4 Jan 93 19:38:59 GMT Raw View
ark@alice.att.com (Andrew Koenig) writes:
>For these reasons, I am dubious about the proposal. I do think, though,
>that it is wise for compilers to issue warning messages for classes
>with virtual functions that do not also have virtual destructors.
I agree. Tis better for compilers to make the programmer more aware,
than to do too much automatically.
--
++harvey
========================================================================
internet: harvey@opl.com / hreed@cs.ulowell.edu / h.reed@ieee.org
voice: 617-965-0220