home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1963 < prev    next >
Encoding:
Text File  |  1993-01-03  |  1.5 KB  |  45 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.std.c++
  4. Subject: virtual destructors by default
  5. Message-ID: <24544@alice.att.com>
  6. Date: 4 Jan 93 03:40:43 GMT
  7. Article-I.D.: alice.24544
  8. Organization: AT&T Bell Laboratories, Murray Hill NJ
  9. Lines: 34
  10.  
  11. There are two problems with extending C++ so that
  12. classes with virtual functions also automatically acquire virtual destructors:
  13.  
  14. 1. It will be a long, long time before a careful programmer can take
  15.    advantage of the feature, because if a program that does so is
  16.    run under an implementation that doesn't support it, it will
  17.    quietly do the wrong thing and the user will blame the author.
  18.  
  19.    In general, extensions that define previously undefined behavior
  20.    (rather than defining previously illegal behavior) must be viewed
  21.    with caution.
  22.  
  23. 2. The proposal doesn't solve the whole problem: it is possible to write
  24.    class definitions that have no virtual functions and nevertheless
  25.    require a virtual destructor.  For instance:
  26.  
  27.    struct Thing { Thing(); ~Thing(); };
  28.    struct B { };
  29.    struct D: B { Thing t; };
  30.  
  31.    main()
  32.    {
  33.     B* bp = new D;
  34.     delete bp;    // Oops!
  35.    }
  36.  
  37.    The commented line is undefined unless D has a virtual destructor.
  38.  
  39. For these reasons, I am dubious about the proposal.  I do think, though,
  40. that it is wise for compilers to issue warning messages for classes
  41. with virtual functions that do not also have virtual destructors.
  42. -- 
  43.                 --Andrew Koenig
  44.                   ark@europa.att.com
  45.