avoid object creation on heap/stack

To prevent accidental creation of an object on the heap:

assign private operators new. For example:
class X {
private:
void *operator new(size_t);
void *operator new[](size_t);
};
To prevent accidental creation on the stack:

make all constructors private, and/or make the destructor private, and provide friend or static functions that perform the same functionality.

For example, here’s one where both constructor and destructor are declared private:
class X {
public:
static X *New() {return new X;}
static X *New(int i) {return new X(i);}
void Delete(X *x) {delete x;}
private:
X();
X(int i);
~X();
};

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *