Limit the creation of object Dynamically

26 January, 2013 - 1 min read

You could try to prevent an object being allocated dynamically ("on the heap") by making its operator new private.

To prevent accidental creation of an object on the heap, give it private operators new. For example:

class X {
    void *operator new(size_t);
    void *operator new[](size_t);

However for preventing someone from creating an instance of your class using placement new.

    void* operator new(size_t);          // standard new
    void* operator new(size_t, void*);   // placement new
    void* operator new[](size_t);        // array new
    void* operator new[](size_t, void*); // placement array new