Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uwm.edu!bionet!ames!sparkyfs!itstd.sri.com!mckenney From: mckenney@fir.itstd.sri.com (Paul E. McKenney) Newsgroups: comp.lang.c++ Subject: Should ``delete this;'' appear in virtual member functions? Message-ID: <30459@sparkyfs.istc.sri.com> Date: 20 Mar 90 01:01:31 GMT Sender: mckenney@sparkyfs.istc.sri.com Reply-To: mckenney@itstd.sri.com (Paul E. McKenney) Organization: SRI International, Menlo Park, CA 94025 Lines: 29 I would like to be able to maintain data structures containing pointers to objects that may have been created by different memory allocators, and would like the function that deletes these data structures to remain ignorant of all of the memory allocators. My first idea was to have a separate derived class for each type of memory allocator, and to make delete be a virtual operator, but this is of course not legal. My second idea is to make a virtual member function that invokes delete, as follows: class blat1 : public blat_base { public: virtual void delete_me() { delete this; } void operator delete(void *p) { dealloc_1(p); } } This seems to work under g++ version 1.36.4, at least as long as the programmer is careful not to deallocate a variable of class blat1 that was allocated statically or automatically. Is this sort of thing portable? Is there some more elegant way to express the concept of user-defined storage class in C++? Thanx, Paul