Ray's playground

 

Item 27: Requiring or prohibiting heap-based objects.(More Effective C++)

The idea is to take advantage of the fact that when an object is allocated on the heap, operator new is called to allocate the raw memory, then a constructor is called to initialize an object in that memory. 

Preventing clients from directly instantiating objects on the heap is easy, because such objects are always created by calls to new and you can make it impossible for clients to call new. Now, you can't affect the availability of the new operator (that's built into the language), but you can take advantage of the fact that the new operator always calls operator new (see Item 8), and that function is one you can declare yourself. In particular, it is one you can declare private.  

 

posted on 2012-03-14 15:06  Ray Z  阅读(177)  评论(0)    收藏  举报

导航