#include <iostream>

using namespace std;

template <typename T>


class MySingleton

{

public:

MySingleton() {}

virtual ~MySingleton() {}



// 이 멤버를 통해서만 생성이 가능하다.

static T* GetSingleton()

{

// 아직 생성이 되어 있지 않으면 생성한다.

if (NULL == _Singleton) {

_Singleton = new T;

}

return (_Singleton);

}

static void Release()

{

delete _Singleton;

_Singleton = NULL;

}

private:

static T* _Singleton;

};

template <typename T> T* MySingleton<T> ::_Singleton = NULL;

// 싱글톤 클래스 템플릿을 상속 받으면서 파라미터에 본 클래스를 넘긴다.

class MyObject : public MySingleton<MyObject>

{

public:

MyObject() : _nValue(10) {}

void SetValue(int Value) { _nValue = Value; }

int GetValue() { return _nValue; }

private:

int _nValue;

};

void main()

{

MyObject* MyObj1 = MyObject::GetSingleton();

cout << MyObj1->GetValue() << endl;

// MyObj2는 Myobj1과 동일한 객체이다.

MyObject* MyObj2 = MyObject::GetSingleton();

MyObj2->SetValue(20);

cout << MyObj1->GetValue() << endl;

cout << MyObj2->GetValue() << endl;

}


빨간색으로 되있는 말 왜 동일한객체라는거?

졸라안보이네 .... 나만어렵나