View Single Post
Go4Expert Member
5Feb2009,12:32  
jayaraj_ev's Avatar
Quote:
Originally Posted by asadullah.ansari View Post
Yes!!! You are right...

Correct code will be as:


Step 5: Introducing reference counts (In our software a class is available for it)


Code:
class Singleton
{
      public:
          static Singleton * CreateSingleton();
          ~Singleton();
          void DestroyMe();
      private:
         static Singleton *instSingleton_mp;
         static int refCount;
         Singleton(){ ; }
         Singleton(const Singleton &) { ; }
};


//Definition 
Singleton* Singleton :: InstSingleton_mp = NULL;
int Singleton :: refCount = 0;


Singleton* Singleton :: CreateSingleton()
{
      if (NULL == InstSingleton_mp)
      {
           InstSingleton_mp = new Singleton(); 
       }

      ++refCount;
      return InstSingleton_mp;
}


Singleton :: DestroyMe()
{

      --refCount;
      if( NULL !=  InstSingleton_mp && 0 == refCount)
     { 
             detele  InstSingleton_mp; 
             InstSingleton_mp=NULL;
     }
}

Code:
Singleton :: DestroyMe()
{

      if(0 < refCount)

         --refCount;
      if( NULL !=  InstSingleton_mp && 0 == refCount)
     { 
             detele  InstSingleton_mp; 
             InstSingleton_mp=NULL;
     }
}


Hi,
You forgaot to add operator overloading for =() to add the counts and subtract the counts when they are assigned to NULL