1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 // FileName : singleton.h 4 // Version : 0.10 5 // Author : Jimmy Han 6 // Date : 2014/6/25 21:54 7 // Comment : 8 // 9 ///////////////////////////////////////////////////////////////////////////////10 11 #ifndef __H__12 #define __H__13 #include <iostream>14 using namespace std;15 16 class Singleton {17 public:18 static Singleton* getInstance(){19 if(uniqueInstance == NULL){20 cout << "synchronized!" << endl;21 if(uniqueInstance == NULL)22 uniqueInstance = new Singleton();23 }24 return uniqueInstance;25 }26 private: 27 static Singleton* uniqueInstance;28 29 Singleton();30 };31 32 33 #endif
1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 // FileName : singleton.cpp 4 // Version : 0.10 5 // Author : Jimmy Han 6 // Date : 2014/6/25 21:54 7 // Comment : 8 // 9 ///////////////////////////////////////////////////////////////////////////////10 11 #include "singleton.h"12 #include <iostream>13 using namespace std;14 15 Singleton* Singleton::uniqueInstance = NULL;16 17 Singleton::Singleton()18 {19 cout << "Singleton class was initilized!" << endl;20 }21 22 23
1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 // FileName : singleton_client.cpp 4 // Version : 0.10 5 // Author : Jimmy Han 6 // Date : 2014/6/25 21:54 7 // Comment : 8 // 9 ///////////////////////////////////////////////////////////////////////////////10 11 #include "singleton.h"12 #include <iostream>13 using namespace std;14 15 int main()16 {17 Singleton* handle = Singleton::getInstance();18 19 return 0; 20 }