Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!tut.cis.ohio-state.edu!ucbvax!pasteur!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: Overloaded operator new VS. inheritance Message-ID: <10351@pasteur.Berkeley.EDU> Date: 21 Jan 91 03:13:54 GMT References: <1991Jan10.195541.6003@usenet@scion.CS.ORST.EDU> <1991Jan10.224933.7518@Neon.Stanford.EDU> <1991Jan17.200447.6794@sco.COM> Sender: news@pasteur.Berkeley.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 32 In article <1991Jan17.200447.6794@sco.COM>, tom@sco.COM (Tom Kelly) writes: > In the example below, the size passed to the destructor is always > only the size of the base class, even when the pointer points > to a derived class. > Note that adding a virtual destructor to the base class fixes > the problem. Ah, but that is correct behavior on the part of the compiler. Without the virtual destructor, the compiler has absolutely no information that the pointer is really to a derived class (what if there are no virtual functions defined in the base class? Then the code has no way, short of ESP, to tell that the pointer is really to a derived class). In short: ALWAYS make the destructor virtual if you plan to derive from the class, and if at any point in your code you have Base* b; ... delete b; but b may point to an object of a different class (shorthand version of this rule: always make the baseclass destructor virtual unless you will never derive from that class). I expect that you'll see identical behavior on any C++ compiler. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck