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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With