Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!fernwood!lia!jgro From: jgro@lia (Jeremy Grodberg) Newsgroups: comp.lang.c++ Subject: Re: Base class vs. derived class and delete Message-ID: <1991Mar15.013138.1341@lia> Date: 15 Mar 91 01:31:38 GMT References: <1991Mar12.202624.19872@bwdls61.bnr.ca> Reply-To: jgro@lia.com (Jeremy Grodberg) Lines: 18 In article <1991Mar12.202624.19872@bwdls61.bnr.ca> gwaters@bwdls35.bnr.ca (Glenn Waters) writes: >The following program does not do what I would hope (expect). The third line of >main does a "delete bp". This seems to only delete String _b in the base class >and *not* String _d in the derived class. Is delete not a "virtual" delete >in that it will delete _d in the derived class? [program omitted] The problem is that you did not declare your destructors to be virtual. delete calls the destructor for the object you are deleting, and if it is not virtual, then it uses the static type. You really should always declare destructors to be virtual for just this reason; the only reason destructors are not *required* to be virtual is so that you can have classes without vtables, so as to be small and C compatable. -- Jeremy Grodberg "Show me a new widget that's bug-free, and I'll show jgro@lia.com you something that's been through several releases."