Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: Overloaded operator new VS. inheritance Summary: Virtual Destructors Message-ID: <469@mole-end.UUCP> Date: 12 Jan 91 11:17:40 GMT References: <1991Jan10.195541.6003@usenet@scion.CS.ORST.EDU> Organization: mole-end--private system. admin: mole-end!newtnews Lines: 46 > I've been using overloaded new and delete operators to implement a "small > object pool" to speed the allocation and deallocation of small objects from > the heap. I have been running into problems with inheritance and want to > propose a simple language extension to deal with the problem. > So what's the problem? > Inheritance. > But then what about the delete operator? The size of the object is > NOT passed to Small::operator delete. I don't know where to put the > recently freed object. Well, actually, it is passed as the (optional) second argument. What you need is to ensure that you get the size for the actual object you have. > It appears that to use overloaded new and delete operators safely you > must either: > 1) remember the object's size within the object itself (requiring > extra memory). > 2) give the object only private constructors, disallowing inheritance. > An easy solution to the problem would be to have Small::operator > delete pass the size of the object --- it's known at the point of > call, even for objects with virtual destructors. You've got it now: virtual destructors. The destructor for the actual (complete and exact) type DOES know the actual size size of the object. If the destructor is virtual, then the operation of operator delete() will (effectively) be virtual as well. (Yes, I know that this is a mistake to speak of a static function as being virtual ... but I write `effectively.') It probably IS a mistake to allow inheritance AND new and delete without a virtual destructor; a derived class which does not inherit a virtual destructor and IS used with operator new, or for which there is an operator delete, probably ought to generate a compiler warning of some sort. This gets rediscovered periodically; it seems like a FAQ item, if ever an FAQ++ there is. -- (This man's opinions are his own.) From mole-end Mark Terribile