Click here to Skip to main content
12,350,443 members (24,190 online)
Click here to Skip to main content
Add your own
alternative version

Stats

81.2K views
478 downloads
24 bookmarked
Posted

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 31659127-Feb-12 15:46
memberMember 31659127-Feb-12 15:46 
Generalwrong way Pin
Kandjar13-Feb-04 3:25
memberKandjar13-Feb-04 3:25 
GeneralRe: wrong way Pin
Cho, Kyung-min13-Feb-04 4:32
memberCho, Kyung-min13-Feb-04 4:32 
GeneralTry it this way instead ... Pin
IanMold12-Aug-02 11:41
memberIanMold12-Aug-02 11:41 
GeneralReally nice... Pin
Jonathan de Halleux20-Aug-02 1:44
memberJonathan de Halleux20-Aug-02 1:44 
GeneralHmm Pin
Thomas Freudenberg8-Aug-02 22:38
memberThomas Freudenberg8-Aug-02 22:38 
GeneralRe: Hmm Pin
Cho, Kyung-min9-Aug-02 1:12
memberCho, Kyung-min9-Aug-02 1:12 
GeneralRe: Hmm Pin
Thomas Freudenberg9-Aug-02 3:25
memberThomas Freudenberg9-Aug-02 3:25 
GeneralRe: Hmm Pin
Robin11-Aug-02 17:18
memberRobin11-Aug-02 17:18 
GeneralRe: Hmm Pin
Cho, Kyung-min11-Aug-02 18:42
memberCho, Kyung-min11-Aug-02 18:42 
GeneralRe: Hmm Pin
Tomasz Sowinski9-Aug-02 6:32
memberTomasz Sowinski9-Aug-02 6:32 
GeneralRe: Hmm Pin
Joao Vaz9-Aug-02 6:57
memberJoao Vaz9-Aug-02 6:57 
GeneralRe: Hmm Pin
Tomasz Sowinski9-Aug-02 7:06
memberTomasz Sowinski9-Aug-02 7:06 
GeneralRe: Hmm Pin
Joao Vaz9-Aug-02 7:21
memberJoao Vaz9-Aug-02 7:21 
GeneralRe: Hmm Pin
Thomas Freudenberg9-Aug-02 7:02
memberThomas Freudenberg9-Aug-02 7:02 
GeneralRe: Hmm Pin
Andreas Saurwein9-Aug-02 6:58
memberAndreas Saurwein9-Aug-02 6:58 
GeneralRe: Hmm Pin
Thomas Freudenberg9-Aug-02 7:07
memberThomas Freudenberg9-Aug-02 7:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 9 Aug 2002
Article Copyright 2002 by Cho, Kyung-min
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid