Singleton (싱글톤)

C++ 2010. 11. 5. 16:58
Thread safety를 위해 Lock 처리 필요.
 

 

Singleton.h
#pragma once

class CSingleton
{
private:
	CSingleton(void);
	CSingleton(const CSingleton& logger);
	CSingleton& operator=(CSingleton& logger);

public:
	~CSingleton(void);

public:
	static CSingleton* inst;
	static CSingleton& getInstance(void);
	static CSingleton* getInstancePtr(void);
	static void destroy(void);
};


Singleton.cpp
#include "Singleton.h"

CSingleton* CSingleton::inst;

CSingleton::CSingleton(void) {}
CSingleton::~CSingleton(void) {}

CSingleton& CSingleton::getInstance(void)
{
	if (inst == 0)
		inst = new CSingleton;

	return *inst;
}

CSingleton* CSingleton::getInstancePtr(void)
{
	if (inst == 0)
		inst = new CSingleton;

	return inst;
}

void CSingleton::destroy(void)
{
	if (inst != 0)
	{
		delete inst;
		inst = 0;
	}
}​



'C++' 카테고리의 다른 글

Memory Pool (메모리 풀)  (1) 2010.11.13
virtual 와 상속 속성  (0) 2010.11.10
stable priority queue  (0) 2010.11.08
log4cxx  (0) 2010.11.06
Win32 메모리 누수(Leak) 체크  (1) 2010.11.04
Distributed Environment (분산 환경)  (0) 2010.11.04
ATL (Active Template Library)  (0) 2010.11.04
ActiveX control  (0) 2010.11.04