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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.