Thursday, September 18, 2008

Restrict dynamic creation of the objects of a class without effecting static creation of objects.

overload new operator and return NULL
-------------------------
example:
class CFoo
{
public:
CFoo(){}
virtual ~CFoo(){}
// overloaded new operator
void* operator new (size_t size); 

// overloaded new[] operator
void* operator new [] (size_t size);
};

void* CFoo :: operator new (size_t  size)
{
return NULL; 
}

void* CFoo :: operator new [] (size_t size)
{
return NULL; 
}

----------------------------------------
note: don't forget to overload new[] also
otherwise, will allow to create dynamic creation when trying to create arrays.

No comments: