Click here to Skip to main content
15,885,546 members
Articles / Mobile Apps
Article

Tiny Singleton helper class

Rate me:
Please Sign up or sign in to vote.
4.17/5 (6 votes)
8 Aug 2002 95.1K   522   24   17
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


Written By
Software Developer
Korea (Republic of) Korea (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 31659127-Feb-12 15:46
Member 31659127-Feb-12 15:46 
Generalwrong way Pin
Kandjar13-Feb-04 3:25
Kandjar13-Feb-04 3:25 
GeneralRe: wrong way Pin
Cho, Kyung-min13-Feb-04 4:32
Cho, Kyung-min13-Feb-04 4:32 
GeneralTry it this way instead ... Pin
IanMold12-Aug-02 11:41
IanMold12-Aug-02 11:41 
GeneralReally nice... Pin
Jonathan de Halleux20-Aug-02 1:44
Jonathan de Halleux20-Aug-02 1:44 
GeneralHmm Pin
Thomas Freudenberg8-Aug-02 22:38
Thomas Freudenberg8-Aug-02 22:38 
GeneralRe: Hmm Pin
Cho, Kyung-min9-Aug-02 1:12
Cho, Kyung-min9-Aug-02 1:12 
GeneralRe: Hmm Pin
Thomas Freudenberg9-Aug-02 3:25
Thomas Freudenberg9-Aug-02 3:25 
GeneralRe: Hmm Pin
Robin11-Aug-02 17:18
Robin11-Aug-02 17:18 
GeneralRe: Hmm Pin
Cho, Kyung-min11-Aug-02 18:42
Cho, Kyung-min11-Aug-02 18:42 
GeneralRe: Hmm Pin
Tomasz Sowinski9-Aug-02 6:32
Tomasz Sowinski9-Aug-02 6:32 
GeneralRe: Hmm Pin
Joao Vaz9-Aug-02 6:57
Joao Vaz9-Aug-02 6:57 
Tomasz Sowinski wrote:
Since C++ does not define the order of initialization for dynamically initialized objects, following code may fail:

You checked the Thomas answer to Cho ?

quoting from Thomas

It may work well as long as both the singleton and the g_nGlobal are in the same translation unit (i.e. .cpp resp. .obj file). But as soon as they are in different units, the initialization order is undetermined, so the code can work correctly, but it's not guaranteed.

This is even a greater issue within Multhreading issues , since Singleton implementation is even harder ...

A known solution is using the double-checked pattern with atomic processor specific instructions (Multiprocessor scenarios) to guarantee proper Singleton behaviour ...






Cheers,
Joao Vaz
A Programming Language is a tool that has profound influence on our thinking habits -The late giant Edsger Dijkstra 1930 - 2002

And if your dream is to care for your family, to put food on the table, to provide them with an education and a good home, then maybe suffering through an endless, pointless, boring job will seem to have purpose. And you will realize how even a rock can change the world, simply by remaining obstinately stationary. -
GeneralRe: Hmm Pin
Tomasz Sowinski9-Aug-02 7:06
Tomasz Sowinski9-Aug-02 7:06 
GeneralRe: Hmm Pin
Joao Vaz9-Aug-02 7:21
Joao Vaz9-Aug-02 7:21 
GeneralRe: Hmm Pin
Thomas Freudenberg9-Aug-02 7:02
Thomas Freudenberg9-Aug-02 7:02 
GeneralRe: Hmm Pin
Andreas Saurwein9-Aug-02 6:58
Andreas Saurwein9-Aug-02 6:58 
GeneralRe: Hmm Pin
Thomas Freudenberg9-Aug-02 7:07
Thomas 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.