Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!apple!bbn!bbn.com!lpringle From: lpringle@bbn.com (Lewis G. Pringle) Newsgroups: comp.lang.c++ Subject: Re: do destructors descend hierarchy? Message-ID: <42280@bbn.COM> Date: 5 Jul 89 18:50:41 GMT References: <1570@cadillac.CAD.MCC.COM> <9555@alice.UUCP> Sender: news@bbn.COM Reply-To: lpringle@labs-n.bbn.com (Lewis G. Pringle) Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 43 In article <9555@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes: >> Blue* b = new Blue; > >In other words, your declarations look like this: > > class Color { /* stuff */ }; > class PrimaryColor: public Color { /* more stuff */ }; > class Blue: public PrimaryColor { /* still more stuff */ }; > >> Now if I: > >> delete b; > >> will the destructors for PrimaryColor and Color be called? > >Yes. This brings up an anoying point about c++. The above works correctly as Andrew points out, but the following: Color* b = new Blue; delete b; Fails in a very bizzare and subtle way. The trouble is that the DTOR was not declared virtual (actually in the example above, the author failed to specify whether or not the dtor was virtual). Is there any rationalle at all (beyond a slight effecicy issue in case of classes with dtors not derived from) for not making dtors always virtual? For sake of symetry with the behavior of ctors it seems quite natural. I expect that a common c++ bug is to ASSUME the dtor of the DYNAMIC class is called when in reallity, only the STATIC type (pointer base type) dtor is used. Lewis. "OS/2: half an operating system for half a computer." In Real Life: Lewis Gordon Pringle Jr. Electronic Mail: lpringle@labs-n.bbn.com Phone: (617) 873-4433