Click here to Skip to main content
Click here to Skip to main content

Tiny Singleton helper class

, 8 Aug 2002
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Cho, Kyung-min
Software Developer
Korea (Republic Of) Korea (Republic Of)
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 31659127-Feb-12 15:46 
Generalwrong way PinmemberKandjar13-Feb-04 3:25 
GeneralRe: wrong way PinmemberCho, Kyung-min13-Feb-04 4:32 
GeneralTry it this way instead ... PinmemberIanMold12-Aug-02 11:41 
GeneralReally nice... PinmemberJonathan de Halleux20-Aug-02 1:44 
GeneralHmm PinmemberThomas Freudenberg8-Aug-02 22:38 
GeneralRe: Hmm PinmemberCho, Kyung-min9-Aug-02 1:12 
GeneralRe: Hmm PinmemberThomas Freudenberg9-Aug-02 3:25 
GeneralRe: Hmm PinmemberRobin11-Aug-02 17:18 
GeneralRe: Hmm PinmemberCho, Kyung-min11-Aug-02 18:42 
GeneralRe: Hmm PinmemberTomasz Sowinski9-Aug-02 6:32 
GeneralRe: Hmm PinmemberJoao Vaz9-Aug-02 6:57 
GeneralRe: Hmm PinmemberTomasz Sowinski9-Aug-02 7:06 
GeneralRe: Hmm PinmemberJoao Vaz9-Aug-02 7:21 
GeneralRe: Hmm PinmemberThomas Freudenberg9-Aug-02 7:02 
GeneralRe: Hmm PinmemberAndreas Saurwein9-Aug-02 6:58 
GeneralRe: Hmm PinmemberThomas Freudenberg9-Aug-02 7:07 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 9 Aug 2002
Article Copyright 2002 by Cho, Kyung-min
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid