IT 취미생활.  
Front Page
Tag | Location | Media | Guestbook | Admin   
 
[펌]Tiny singleton helper class

Introduction

Sometimes we have to code with 'singleton'. If you don't know what Singleton is, you can read about it in many Design Pattern books. Singleton principle is really simple.

As you know, if you use reference pointer for singleton object, then you must delete it when program terminates. But it will be good because it does 'late instance'.

I want to terminate singleton object automatically, and instance lately. So, I coded this.

Good things:

  • Late instance.
  • Safe terminate automatically.
  • Separated code for Object and Singleton.

Here is my tiny singleton helper class - CSingletonT<>, and I hope it helps you.

//
// Using singletonT class
//
#include "SingletonT.h"
// test class
class CObj
{
protected:
    CObj(){ x = 0 ; TRACE("Created CObj\r\n"); }
public:
    virtual ~CObj(){ x = 0 ; TRACE("Deleted CObj\r\n");}
    int x;
};
// Testing
void CTestSingleTDlg::OnButton1()
{
    // TODO: Add your control notification handler code here
    // if first call, then singleton object will instance ( instance lately )
        CObj* o = CSingletonT<CObj>::GetObject();
    // use singletone object
    o->x ++;
    TRACE("o->x = %d\r\n",o->x);
}
------------------------------------------------------------------------------------
싱글톤에 조금은 벗어났지,활용 할 수 있는 방법은 많다고 생각 됩니다.
원문을 보고 싶으시다면  아래로...
http://www.codeproject.com/cpp/singlet.asp
우리 나라 분이 작성한 글입니다.


BLOG main image
취미생활
 Notice
 Category
분류 전체보기 (191)
매일매일갱생 (83)
서버개발 (1)
임베디드개발 (12)
Programming (80)
Personal Projects (6)
유용한 프로그램 (0)
 TAGS
1seg Linux Java 티스토리 초대장 벨소리 변경 Debug 미라지폰 spam mail Windows Mobile6.0 It debugging DirectShow 영어 이메일 음식 개발자 Wince5.0 출장 Error Case MP3 project english email C 티스토리초대 isdbt 퇴사 DVB ISDB-T 서태지 VC++ C++ Algorithm M480 알고리즘 Brazil Dshow English DVB-T warning 군대
 Calendar
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
 Recent Entries
 Recent Comments
 Recent Trackbacks
 Archive
 Link Site
zextor
괴짜 프로그래머의 일상사~@@
Gag & Peace, and more..
Kazakhstan Almaty.......
Min-A
Sadgarret
Steve Yoon's log
가슴 뛰는 삶을 살아라
오스틴 파워
GUI sin
melanie parker_Lady
제레미의 TV 2.0 이야기..
 Visitor Statistics
Total :
Today :
Yesterday :
rss