Overloading The Operator new

From Appmethod Topics
Jump to: navigation, search

Go Up to The new And delete Operators Index

The global ::operator new() and ::operator new[]() can be overloaded. Each overloaded instance must have a unique signature. Therefore, multiple instances of a global allocation operator can coexist in a single program.

Class-specific memory allocation operators can also be overloaded. The operator new can be implemented to provide alternative free storage (heap) memory-management routines, or implemented to accept additional arguments. A user-defined operator new must return a void* and must have a size_t as its first argument. To overload the new operators, use the following prototypes declared in the new.h header file.

void * operator new(size_t Type_size);     // For Non-array
void * operator new[](size_t Type_size);   // For arrays

The compiler provides Type_size to the new operator. Any data type may be substituted for Type_size except function names (although a pointer to function is permitted), class declarations, enumeration declarations, const, volatile.

See Also