Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!nrl-cmf!ames!apple!bloom-beacon!mit-eddie!apollo!vasta From: vasta@apollo.COM (John Vasta) Newsgroups: comp.lang.c++ Subject: Re: Destructors and new Summary: another case for virtual destructors Keywords: virtual destructor Message-ID: <439857c0.1ad5a@apollo.COM> Date: 2 Jun 89 19:33:00 GMT References: <2685@ssc-vax.UUCP> Reply-To: vasta@apollo.COM (John Vasta) Organization: Apollo Computer, Chelmsford, MA Lines: 31 In article <2685@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: > > I need some help. In the process of learning C++ for use on a project, >I am developing some "basic" classes. First of all, I developed a string >class, and then I went on to developing a "list" class. I've included some >of the code from my .hxx file: > > [code example deleted] > > Ok, this works too - sort of. The problem is that in the example using >a "list", the destructors for s1, s2 and s3 are called. However, in >the example using "dlist", no destructors are called for the instances >of strings that get appended onto the list. Notice that the dlist destructor >calls delete to free the memory associated with each string, but the >explicit destructor for strings is not called. > > Any suggestions? Sounds like you've tuned in recently - you may have missed the big virtual destructor discussion. Yes, destructors can be declared virtual (in recent implementations, at least). Your dlist destructor only knows that it's destroying an object pointed to by a link*, and the link class has no destructor, so there's nothing to do. Declare a virtual destructor in the link class, and then classes derived from link will have their destructors called when deleting via a ptr-to-link. -- John Vasta Apollo Computer, Inc. vasta@apollo.com M.S. CHA-01-LT (508) 256-6600 x6362 330 Billerica Road, Chelmsford, MA 01824 UUCP: {decwrl!decvax, mit-eddie, attunix}!apollo!vasta