Item 34. Restricting Heap Allocation

Sometimes it’s a good idea to indicate that objects of a particular class should not be allocated on the heap. Often this is to ensure that the object’s destructor is called, as in the case of a “handle” object that maintains a reference count for a “body” object. Local objects with automatic storage class will have their destructors called automatically (except in the case of abnormal program termination via an exit or abort), as will objects with static storage class (except in the case of an abort), whereas heap-allocated objects must be destroyed explicitly.

One way to indicate such a preference is by defining heap memory allocation to be illegal:

Any conventional attempt to allocate a NoHeap object ...

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.