// 싱글톤 : 객체가 하나만 생성되게 한다.


// Mayer
singleton( stack 이용 )

class MouseCursor

{

private:

        MouseCursor() {}

public:

        static MouseCursor& GetInstance()

        {

               static MouseCursor m;

               return m;

        }

};

 

void main()

{

        MouseCursor& m1 = MouseCursor::GetInstance();

        MouseCursor& m2 = MouseCursor::GetInstance();

}

//heap 공간에singleton 구현하기

#include <iostream>

using namespace std;

 

class Singleton

{

private:

        static Singleton* m_cursor;

        Singleton() {}

        Singleton(const Singleton&);

        ~Singleton()

        {

               if ( m_cursor != NULL )

               {

                       delete m_cursor;

               }

        } 

 

public:

        static Singleton* GetInstance()

        {

               if (m_cursor == NULL)

                       m_cursor = new Singleton();

               return m_cursor;

        }

};

 

Singleton* Singleton::m_cursor = NULL;

 

void main()

{

        Singleton* single1 = Singleton::GetInstance();

        Singleton* single2 = Singleton::GetInstance();

}


// Double Checked Locking Singleton( 윈도우- 멀티스레드환경대비)

// 객체를 생성할 때 CRITICAL_SECTION 으로 감싸준다.
#include
<iostream>

#include <windows.h>

using namespace std;

 

class CriticalSection

{

private:

        CRITICAL_SECTION cs;

public:

        CriticalSection() { InitializeCriticalSection(&cs); }

        ~CriticalSection() { DeleteCriticalSection(&cs); }

 

        void Enter()

        {

               EnterCriticalSection(&cs);

        }

        void Leave()

        {

               LeaveCriticalSection(&cs);

        }

};

 

CriticalSection g_cs;

 

class Singleton

{

private:

        static Singleton* m_cursor;

       

        Singleton() {}

        Singleton(const Singleton&);

        ~Singleton()

        {

               if ( m_cursor != NULL )

               {

                       delete m_cursor;

               }

        } 

 

public:

        static Singleton* GetInstance()

        {

               if (m_cursor == NULL)

               {

                       m_cursor = new Singleton();

               }

               return m_cursor;

        }

};

 

Singleton* Singleton::m_cursor = NULL;

 

DWORD WINAPI SingletonProc(void* pv)

{

        Singleton* pSingle = NULL;

 

        while (1)

        {

               if (pSingle == NULL)

               {

                       g_cs.Enter();

                       pSingle = Singleton::GetInstance();

                       cout << "Singleton create : " << pSingle << endl;

                       g_cs.Leave();

               }

               Sleep(0);

        }

        return 0;

}

 

void main()

{

        HANDLE hThread[2];

 

        hThread[0] = CreateThread( NULL, 0, SingletonProc, NULL, 0, NULL );

        hThread[1] = CreateThread( NULL, 0, SingletonProc, NULL, 0, NULL );

 

        WaitForMultipleObjects(2, hThread, TRUE, INFINITE);

        CloseHandle(hThread[0]);

        CloseHandle(hThread[1]);

}

Tag |

7.26(목) C++ 기초문법 - static 멤버

from Study/C++ 2007/07/28 23:53 view 20184

// 1. static 멤버data의특징: 모든객체가공유. 객체가없어도메모리에있다.

// 2. static 멤버함수의특징객체가없어도호출가능.

// 3. static 멤버에서는static 멤버만접근가능.

class Point

{

        int x;

        static int y;

 

public:

        void foo()

        {

               x = 10  // 1

               y = 10; // 2

        }

        static void goo()

        {

               cout << this << endl;  // error

               foo();  // error

               x = 10; // error

               y = 10; // 4

        }

};

 

int Point::y = 0;

 

void main()

{


        //클래스 내의 static 멤버 호출 시 1번으로 한다.
       
Point::goo();  // 1
일반함수인지static 함수인지를알수있다.

 

        Point p;

        p.goo();      // 2

}

 

Tag |