Overloading new, removal in C ++

I came across this line: strustrup 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).

Does the dont new operator and the delete operator accept a user-defined type as one of their arguments? what does it mean i missed something here

+3
source share
4 answers

The quote from Stroustrup seems to refer to operator overloading. C ++ supports operator overloading only for user types. This means that the overload ( operator <something>) function must either be a member of a user type or be a standalone function with at least one user type argument. This is exactly what is meant by quote.

( ) operator new operator delete . , .

. . operator new/operator delete, . : , . operator new operator delete.

+5

new, (, ), , new, , malloc() operator new, .

+3

a + b - a.operator+(b) operator+(a, b).

, new Foo(x, y, z) operator new(Foo, x, y, z) - . :

void* address = operator new(sizeof(Foo)); // here is the behavior you can replace
try {
    new(address) Foo(x, y, z);
} catch (...) {
    operator delete(address);
}

, operator new , , new. , allocate_memory - . operator+.

+2

operator new operator delete new delete new. , ( operator new ), , . (++ 03 ยง13.5/5)

, . operator new size_t (, , , ) void *. operator delete void * size_t, , .

:

  • They deal with raw memory that does not contain the constructed object. There is always an error for operator newor operator deleteto try to access the object in the returned memory block. (Memory not used to create objects, however, is fair play.)
  • Class member operator newor operator deletecan be recycled and / or actually inherited, so that the subject void*can not point to a not yet constructed or already destroyed subobject any voodoo.
0
source

Source: https://habr.com/ru/post/1743765/


All Articles