How to enforce users to create objects of class derived from mine with "new" only?

Posted by sharptooth on Stack Overflow See other posts from Stack Overflow or by sharptooth
Published on 2010-05-14T11:30:20Z Indexed on 2010/05/14 11:34 UTC
Read the original article Hit count: 249

Filed under:
|
|

To implement reference counting we use an IUnknown-like interface and a smart pointer template class. The interface has implementation for all the reference-count methods, including Release():

void IUnknownLike::Release()
{
   if( --refCount == 0 ) {
       delete this;
   }
}

The smart pointer template class has a copy constructor and an assignment operator both accepting raw pointers. So users can do the following:

 class Class : public IUnknownLike {
 };

 void someFunction( CSmartPointer<Class> object ); //whatever function
 Class object;
 someFunction( &object );

and the program runs into undefined behavior - the object is created with reference count zero, the smart pointer is constructed and bumps it to one, then the function returns, smart pointer is destroyed, calls Release() which leads to delete of a stack-allocated variable.

Users can as well do the following:

struct COuter {
    //whatever else;
    Class inner;// IUnknownLike descendant
};
COuter object;
somefunction( &object.Inner );

and again an object not created with new is deleted. Undefined behavior at its best.

Is there any way to change the IUnknownLike interface so that the user is forced to use new for creating all objects derived from IUnknownLike - both directly derived and indirectly derived (with classes in between the most derived and the base)?

© Stack Overflow or respective owner

Related posts about c++

Related posts about smart-pointers