Search code examples
c++classmemory-managementheap-memorystack-memory

why would I forbid allocation in the heap?


I recently read a lot about "preventing heap allocation for a class" (see this question).

I was able to understand "how", but now I can't figure out "why" someone would like to do that.

I guess there must be legitimate reasons for this, but I just can't figure them out.

In short: "Why may I want to forbid users from creating objects of my class in the heap ?"


Solution

  • Some classes make sense only if the objects are instantiated on the stack. For example, Boost scoped_ptr, or lock_guard.