Short answer:
Operators
new and delete overloaded in the class area to optimize the distribution for objects of a particular class. But there may be special scenarios due to certain animals, such as Inheritance , which can lead to distribution requests larger than the class size itself. Since the most important task of overloading new and delete is a special setting for objects of size sizeof(Base) , nothing more or less, these overloaded operators must forward all other wrong sized memory requests to ::operator new and ::operator delete in order to be able to To do this, the size parameter must be passed as a parameter.
Long answer:
Consider a special scenario:
class Base { public: static void * operator new(std::size_t size) throw(std::bad_alloc); }; class Derived: public Base {
In the above example, due to inheritance, the Derived derived class inherits the new Base class statement. This makes the call statement new in the base class to allocate memory for the object of the derived class. The best way for a new operator to cope with this situation is to forward such calls requesting the "wrong" amount of memory for a standard new operator, for example:
void * Base::operator new(std::size_t size) throw(std::bad_alloc) { if (size != sizeof(Base))
When overloading the delete operator, you must also make sure that since the class operator for new requests sends the "wrong" size to ::operator new , one MUST send the "wrong size" requests to delete the operator: Since the original operators are guaranteed to process these requests in a standard way.
Thus, the user delete operator would be something like this:
class Base { public: //Same as before static void * operator new(std::size_t size) throw(std::bad_alloc); //delete declaration static void operator delete(void *rawMemory, std::size_t size) throw(); void Base::operator delete(void *rawMemory, std::size_t size) throw() { if (rawMemory == 0) { return; // No-Op is null pointer } if (size != sizeof(Base)) { // if size is "wrong," ::operator delete(rawMemory); //delegate to std::delete return; } //If we reach here means we have correct sized pointer for deallocation //deallocate the memory pointed to by rawMemory; return; } };
Further reading:
The following C ++ entry - Faq talks about overloading the new and deleting in a standard way and may be well read for you:
How to write standard standard and remote iso C ++ statements?
Alok save
source share