Click here to Skip to main content
Licence 
First Posted 7 Aug 2002
Views 72,424
Bookmarked 22 times

Tiny Singleton helper class

By | 8 Aug 2002 | Article
SingleT will provide you the easy way to get Singleton pattern.

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

Software Developer

Korea (Republic Of) Korea (Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMember 316591215:46 7 Feb '12  
Generalwrong way PinmemberKandjar3:25 13 Feb '04  
GeneralRe: wrong way PinmemberCho, Kyung-min4:32 13 Feb '04  
GeneralTry it this way instead ... PinmemberIanMold11:41 12 Aug '02  
GeneralReally nice... PinmemberJonathan de Halleux1:44 20 Aug '02  
GeneralHmm PinmemberThomas Freudenberg22:38 8 Aug '02  
GeneralRe: Hmm PinmemberCho, Kyung-min1:12 9 Aug '02  
GeneralRe: Hmm PinmemberThomas Freudenberg3:25 9 Aug '02  
GeneralRe: Hmm PinmemberRobin17:18 11 Aug '02  
GeneralRe: Hmm PinmemberCho, Kyung-min18:42 11 Aug '02  
GeneralRe: Hmm PinmemberTomasz Sowinski6:32 9 Aug '02  
Thomas Freudenberg wrote:
Since C++ does not define the order of initialization for dynamically initialized objects, following code may fail:
 
Why? AFAIK, the constructor for static object declared in GetObject will be invoked on first call to GetObject(). Am I missing something?
 
Tomasz Sowinski -- http://www.shooltz.com

What is "scratch" and why can everything be made from it?

GeneralRe: Hmm PinmemberJoao Vaz6:57 9 Aug '02  
GeneralRe: Hmm PinmemberTomasz Sowinski7:06 9 Aug '02  
GeneralRe: Hmm PinmemberJoao Vaz7:21 9 Aug '02  
GeneralRe: Hmm PinmemberThomas Freudenberg7:02 9 Aug '02  
GeneralRe: Hmm PinmemberAndreas Saurwein6:58 9 Aug '02  
GeneralRe: Hmm PinmemberThomas Freudenberg7:07 9 Aug '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 9 Aug 2002
Article Copyright 2002 by Cho, Kyung-min
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid