Click here to Skip to main content
6,595,444 members and growing! (21,497 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Tiny Singleton helper class

By Cho, Kyung-min

SingleT will provide you the easy way to get Singleton pattern.
VC6, VC7.NET 1.0, Win2K, WinXP, PocketPC 2002, Dev
Posted:7 Aug 2002
Updated:8 Aug 2002
Views:64,406
Bookmarked:18 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.07 Rating: 3.07 out of 5
1 vote, 20.0%
1

2

3
1 vote, 20.0%
4
3 votes, 60.0%
5

Sample Image

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);
}

If it works, then you can see output like in the sample picture:

Sample Image

I hope it helps you, Thanks!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Cho, Kyung-min


Member
I born in 1978 and live in korea.
I have to study hard to be better programmer. if my shy code help someone, then i hope share it with pleasure. My nick is 'bro' ,(that is) short for 'brother'. i like it cuz feel friendly. Thanks for reading.

I'm working for 5yrs in company. now in samsung electronics.

my first c 'hello world' is in 1995. and now my interesting is about serverside stuff( iocp, 3tier, etc.) and rtos.

Occupation: Web Developer
Location: Korea, Republic Of Korea, Republic Of

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Generalwrong way PinmemberKandjar4:25 13 Feb '04  
GeneralRe: wrong way PinmemberCho, Kyung-min5:32 13 Feb '04  
GeneralTry it this way instead ... PinmemberIanMold12:41 12 Aug '02  
GeneralReally nice... PinmemberJonathan de Halleux2:44 20 Aug '02  
GeneralHmm PinmemberThomas Freudenberg23:38 8 Aug '02  
GeneralRe: Hmm PinmemberCho, Kyung-min2:12 9 Aug '02  
GeneralRe: Hmm PinmemberThomas Freudenberg4:25 9 Aug '02  
GeneralRe: Hmm PinmemberRobin18:18 11 Aug '02  
GeneralRe: Hmm PinmemberCho, Kyung-min19:42 11 Aug '02  
GeneralRe: Hmm PinmemberTomasz Sowinski7:32 9 Aug '02  
GeneralRe: Hmm PinmemberJoao Vaz7:57 9 Aug '02  
GeneralRe: Hmm PinmemberTomasz Sowinski8:06 9 Aug '02  
GeneralRe: Hmm PinmemberJoao Vaz8:21 9 Aug '02  
GeneralRe: Hmm PinmemberThomas Freudenberg8:02 9 Aug '02  
GeneralRe: Hmm PinmemberAndreas Saurwein7:58 9 Aug '02  
GeneralRe: Hmm PinmemberThomas Freudenberg8:07 9 Aug '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Aug 2002
Editor: Smitha Vijayan
Copyright 2002 by Cho, Kyung-min
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project