Industrial Training

Overloading new and delete



Overloading new and delete _ A class may organize its own memory management by overloading the new and delete operators. There are a number of special requirements that must be satisfied by operator functions defined for new and delete.

Operator new:
A class member operator function for operator new must have a return type of void* and take a first argument of type size_t. This argument is AUTOMATICALLY set by the compiler with the class size in bytes.

Operator delete:
A class member operator function for operator delete must have a first argument of type void*. A second optional argument of size_t may be specified. If present, it is initialized by the compiler with the size in bytes of the object which is addressed by the first argument.

Note that the default instance of operator new is always used for the allocation of memory for arrays of class objects. The scope resolution operator may be used to force the default global instance of operators new or delete to be used, even when a class instance is defined, as in: ::delete p; Operators new and delete are automatically assumed by the compiler to be static members of their class. This means that the this pointer is undefined within their scope and thus they can directly access only static data members of the class. This is necessary since they may be invoked when no objects of the class exist.

Hi I am Pluto.