home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark
- From: ark@alice.att.com (Andrew Koenig)
- Newsgroups: comp.std.c++
- Subject: virtual destructors by default
- Message-ID: <24544@alice.att.com>
- Date: 4 Jan 93 03:40:43 GMT
- Article-I.D.: alice.24544
- Organization: AT&T Bell Laboratories, Murray Hill NJ
- Lines: 34
-
- 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
-