home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: virtual destructor warning
- Message-ID: <1992Sep7.005034.21889@taumet.com>
- Organization: TauMetric Corporation
- References: <18dhgaINN4r7@usenet.INS.CWRU.Edu>
- Date: Mon, 7 Sep 1992 00:50:34 GMT
- Lines: 39
-
- esw@po.CWRU.Edu (Eric S. Wallace) writes:
-
- [condensed]
-
- |class ibuf {
- |public: ...
- | ~ibuf(void) { /* ... */ }
- |};
-
- |class obuf {
- |public: ...
- | ~obuf(void) { /* ... */ }
- |};
-
- |class iobuf : public obuf, public ibuf { ... }
-
- |When compiling with gcc-2.2.2, I get the following warning:
- | destructor `ibuf::~ibuf()' non-virtual in relationship `iobuf: ibuf'
-
- | ...
- | Why would ibuf::~ibuf have to be virtual?
-
- Destructors don't have to be virtual, which is why you got a warning
- rather than an error message. If destructors are not virtual, you risk
- losing polymorphism. Example:
- ibuf* = new iobuf(...);
- ...
- delete ibuf;
- In this case, the iobuf destructor is not called, and hence the obuf
- destructor is not called either. If the base class destructors were
- virtual, the destructors for the actual constructed class would be
- called, which is normally what you want.
-
- Cases where you want non-virtual destructors in base classes are rare,
- and g++ is just being helpful in reminding you of that.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-