::NPTEAM:: Network Programer Team

검색 :
RSS 구독 : 글 / 댓글 / 트랙백 / 글+트랙백

[C++] 템플릿 기반의 싱글톤 클래스




// Singleton class
#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;


#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;
}
 
2007/05/06 04:22 2007/05/06 04:22

맨 위로