티스토리 뷰

공부 이야기

[C] 싱글톤 클래스 구성

판다(panda) 2009. 1. 30. 00:02

//싱글톤 클래스

#include "stdafx.h"
#include <stdio.h>


class A
{
public:
    static A *Create();
    static A *GetSingleTone() { return ms_pkThis; }
private:
    static A *ms_pkThis;
};

A *A::Create()
{
    if ( ms_pkThis )
    return ms_pkThis;                  // ms_pkThis 있으면 반환 없으면 새로 생성
    ms_pkThis = new A;
    return ms_pkThis;
}

A *A::ms_pkThis = 0;

int _tmain(int argc, _TCHAR* argv[])
{
    A *p = A::Create();
    printf("Addr : %x\n" ,p);

    p = A::Create();
    printf("Addr : %x\n" ,p);

    p = A::GetSingleTone();
    printf("Addr : %x\n" ,p);
    delete p;
    return 0;
}

실행 결과..