Item 36. Class-Specific Memory Management

If you don’t like the way standard operator new and operator delete are treating one of your class types, you don’t have to stand for it. Instead, your types can have their own operator new and operator delete customized to their needs.

Note that we can’t do anything with the new operator or the delete operator, since their behavior is fixed, but we can change which operator new and operator delete they invoke (see Placement New [35, 119]). The best way to do this is to declare member operator new and operator delete functions:

image

When we allocate an object of type Handle in a new expression, the compiler ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.