1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #pragma once template < typename t= "" > class CSingleton { public : static T * InstancePtr() { if ( ms_Instance == NULL ) ms_Instance = new T; return ms_Instance; }; static T & Instance() { if (ms_Instance == NULL) ms_Instance = new T; return *ms_Instance; }; static void DestroyInstance() { delete ms_Instance; ms_Instance = NULL; }; private : static T * ms_Instance; }; template < typename t= "" > T* CSingleton<t>::ms_Instance = 0; </t></ typename ></ typename > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include "stdafx.h" #include <iostream> #include "CSingleton.h" class CObject : public CSingleton<cobject> { public : CObject() : m_nValue(10) {} ~CObject(); int m_nValue; int GetValue() { return m_nValue; } }; #define TIME CObject::Instance() int _tmain( int argc, _TCHAR* argv[]) { int time = TIME.GetTime(); return 0; } </cobject></iostream> |
덧글을 달아 주세요