//A class can delegate the maintenance of its reference count to its
//base class 'ReferenceCounter'. Once the reference count drop to zero
//the class object can be deleted. Here you can see: delete this ;
class ReferenceCounter
{
private: int m_refCounter ;
public:
ReferenceCounter( ) : m_refCounter ( 0 ) { }
void AddRef( ) { m_refCounter++ ; }
void Release( ) {
if ( m_refCounter == 0 ) { return ; }
if ( --m_refCounter == 0 ) { delete this ; }
}
} ;
class MyClass : public ReferenceCounter
{
public : MyClass( ) { AddRef( ) ; }
public : MyClass * getInstance( ) { AddRef( ) ; return this; }
void ReleaseInstance ( ) { Release( ) ; }
} ;