home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13353 < prev    next >
Encoding:
Text File  |  1992-09-08  |  1.4 KB  |  50 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: virtual destructor warning
  5. Message-ID: <1992Sep7.005034.21889@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <18dhgaINN4r7@usenet.INS.CWRU.Edu>
  8. Date: Mon, 7 Sep 1992 00:50:34 GMT
  9. Lines: 39
  10.  
  11. esw@po.CWRU.Edu (Eric S. Wallace) writes:
  12.  
  13.  [condensed]
  14.  
  15. |class ibuf {
  16. |public: ...
  17. |  ~ibuf(void) { /* ... */ }
  18. |};
  19.  
  20. |class obuf {
  21. |public: ...
  22. |  ~obuf(void) { /* ... */ }
  23. |};
  24.  
  25. |class iobuf : public obuf, public ibuf { ... }
  26.  
  27. |When compiling with gcc-2.2.2, I get the following warning:
  28. |  destructor `ibuf::~ibuf()' non-virtual in relationship `iobuf: ibuf'
  29.  
  30. | ...
  31. | Why would ibuf::~ibuf have to be virtual?
  32.  
  33. Destructors don't have to be virtual, which is why you got a warning
  34. rather than an error message.  If destructors are not virtual, you risk
  35. losing polymorphism.  Example:
  36.     ibuf* = new iobuf(...);
  37.     ...
  38.     delete ibuf;
  39. In this case, the iobuf destructor is not called, and hence the obuf
  40. destructor is not called either.  If the base class destructors were
  41. virtual, the destructors for the actual constructed class would be
  42. called, which is normally what you want.
  43.  
  44. Cases where you want non-virtual destructors in base classes are rare,
  45. and g++ is just being helpful in reminding you of that.
  46. -- 
  47.  
  48. Steve Clamage, TauMetric Corp, steve@taumet.com
  49. Vice Chair, ANSI C++ Committee, X3J16
  50.