Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.c++ Subject: proposal for new/delete extension Message-ID: <19215@prometheus.megatest.UUCP> Date: 25 Jun 91 23:59:31 GMT Organization: Megatest Corporation, San Jose, Ca Lines: 56 I would like to describe to you a small language-extension which I think would prove useful. First I will describe the problem to be solved. Occasionally, for the sake of efficiency, it is desirable to allocate memory for new objects of a given class from a pool of preallocated packets, sized by the sizeof() operator. It has been my experience that this practice can improve the performance of some applications very significantly. We therefore redefine the "new" and "delete" operators for the class. class Class1 { char goodies[SIZE]; public: void *operator new(size_t); // ignores the size_t void operator delete(void *); // etc.. }; In general the new and delete declarations must be public, although classes that are used only by a select number of friend-classes can declare the the operators as private. The problem here is that classes derived from Class1 will inherit the new and delete operators. Because those operators manage a pool of fixed-size packets, the inheritance is faulty. It implies implicit casts between void* and each of the base and derived classes, although only the cast between void* and the base class is valid. class Bogus: public Class1 { char more_goodies[SIZE2]; public: // etc. }; The above class Bogus will probably be allocated only "SIZE" bytes of memory by the (inherited) operator new. Its actual size is likely to be SIZE + SIZE2. I recommend an alternate way to specify class-specific operators new and delete which stipulates that they must not be inherited: class Class1 { char goodies[SIZE]; public: Class1 *operator new(); void operator delete(Class1*); }; Notice that this version of operator new does not take a size_t parameter. Notice also that both the declarations for both operators specify that they manage memory specifically for Class1, not for (void*) classes of arbitrary size and alignment. Operators declared this way would override the default or inherited operators new and delete in Class1 only, not in derived classes. Stated another way, there would be no implicit casts of void* implied by these operators.