Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!nrl-cmf!ames!pasteur!agate!violet.berkeley.edu!pete From: pete@violet.berkeley.edu (Pete Goodeve) Newsgroups: comp.lang.c++ Subject: Re: destructors, derived classes, and pointers Summary: Thanks (but I'm not totally happy..) Message-ID: <16250@agate.BERKELEY.EDU> Date: 31 Oct 88 04:46:50 GMT References: <16219@agate.BERKELEY.EDU> <1285@cod.NOSC.MIL> Sender: usenet@agate.BERKELEY.EDU Organization: University of California, Berkeley Lines: 36 Mike Ball [in <1285@cod.NOSC.MIL>] answers my screams of anguish: > >The problem is that all the managing procedure has is a pointer to the > >object (of the base type naturally). Now I would expect that a delete call > >on this pointer would detect the correct destructor to invoke before it > >actually disposed of the memory, but NOOO! It calls ONLY the parent type > >destructor!!! So much for nice neat cleanup... > > > Make your destructor virtual and all will work as you wish...... Hmmm. Yup, that works. Thanks. Actually I thought I'd tried that in the course of my rushing in five directions at once... I was probably fooled by the fact that virtual CONstructors are illegal. Also my only reference at the moment is Wiener and Pearson, who don't mention that possibility. [It seems that our local bookstores are out of Stroustrup's book at the moment. Is that maybe a good sign? On the other hand, the UC Berkeley library doesn't have a SINGLE copy!] I'm still not entirely happy, because this solution seems particularly ad hoc. I don't like Ifs Ands and Buts in a language. I can't see that you would EVER want ONLY the parent class destructor to be invoked, so that there should be some protection against this. I realize that this is a case where efficiency has taken precedence over security, but I'm not sure this was a wise choice. Could the compiler raise an error flag if you ever assigned a derived object with a destructor to a pointer of the base class WITHOUT a virtual destructor? I think this might solve the dilemma. As a final carp, there is still the other inconsistency I noted [section (1) of my last message] in that a 'this->' reference in a parent constructor picks up the PARENT's virtual function, rather than the derived class function that would be invoked in any other case. This surely is wrong? -- Pete --