Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading new, delete in C++

i came across this line is stroustrup An operator function must either be a member or take at least one argument of a user-defined type (functions redefining the new and delete operators need not).

Dont operator new and operator delete take an user defined type as one of their arguments? what does it mean, am i missing something here

like image 523
shreyasva Avatar asked Dec 28 '22 18:12

shreyasva


1 Answers

The quote from Stroustrup apparently applies to operator overloading. C++ language supports operator overloading for user-defined types only. This means that the overloading function (operator <something>) has to be either a member of user-defined type or be a standalone function with at least one argument of user-defined type. This is exactly what is meant by the quote in question.

Yet standalone (non-member) operator new and operator delete functions are not required to take a value of user defined type as one of their arguments. This might be seen as something that contradicts your quote.

However, in reality there's no contradiction. These operators are not really overloaded. When you provide your own versions of standalone operator new/operator delete, you are actually replacing the library-provided ones. This is the official term from the language specification: replacement, not overloading. Which is why the above quote does not really apply to operator new and operator delete.

like image 158
AnT Avatar answered Jan 12 '23 09:01

AnT