C++; Singleton Design Pattern

Singleton is a creational design pattern.
Provide one and only instance of an object.

#include using namespace std; 
class MySingleton 
      public: static MySingleton* iInstance; 
      public: static MySingleton* GetInstance(); 
      private: MySingleton(); 
}; 
MySingleton* MySingleton::iInstance = NULL; 
MySingleton::MySingleton() 
      cout << "Inside construtor ..." << endl; 
MySingleton* MySingleton::GetInstance() 
{
      if ( iInstance == NULL ) 
      {  
           iInstance = new MySingleton(); 
      } 
     return iInstance; 


void main() 
     MySingleton* obj; 
     obj = MySingleton::GetInstance(); 
}

No comments:

Post a Comment