Click here to Skip to main content
Licence 
First Posted 19 Mar 2003
Views 103,586
Bookmarked 28 times

A Singleton Template Class

By | 19 Mar 2003 | Article
A Singleton Template Class

Introduction

There are times, when you need to have a class which can be instantiated once only. The Singleton Design Pattern provides a solution for such a situation.

There are several possible ways to implement a singleton pattern, but it all pretty much comes down to a class that has a private constructor and a static member function to create and retrieve an instance of the class. My implementation does not differ much from this scenario, with the exception that I created a singleton template class.

So, why a template class?

Well I have searched the Internet for an elegant implementation of a singleton class but I did not really find a solution to my satisfaction. Most classes I found consist of a Singleton base class that you can use to derive your own singleton class from. The problem with most of these classes is the fact that you still have to override the GetInstance function to return a pointer to your derived class. A template base class does not have this limitation as I can return any type of pointer.

How it works

To prevent outside source from creating (or copying) an instance of our singleton class, we need to shield the constructor and copy constructor of the singleton class. Further we need to provide a method to create and retrieve a reference to the singleton object:

static T* Instance()
{
if (m_instance == NULL) m_instance = new T;
            ASSERT(m_instance != NULL);
return m_instance;
};

When this method is called for the first time, it creates an instance of the singleton class, any sequential calls will return a reference to the created class instance. To get a reference to the singleton object, all we have to do is call this method as following:

CMySingleton* mySingleton = CMySingleton::Instance();

That is almost all that there is to it. Next to shielding the constructors, I also shielded the destructor, so the singleton class cannot be deleted by accident. Just call the DestroyInstance() method to destroy the singleton object. However, be careful when to call this method, because after you have called this method, all your class data will be destroyed and a sequential to the Instance() method will create a new instance.

So how do you create a class derived from the singleton template class? Again there is nothing to it. Just include the attached header file and create your object as following:

class CMySingleton : public CSingleton<CMySingleton>
{
    friend CSingleton<CMySingleton>;   
 
       private:      

           CMySingleton();
           ~CMySingleton();
 
    ...
}

Conclusion

This implementation of the Singleton Pattern makes creating your own singleton class incredibly easy. But you do have to be careful when to destroy the singleton class instance. If you find this to be a problem you could consider adding (automatic) reference counting.

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

Brian van der Beek

Web Developer

Netherlands Netherlands

Member

Brian van der Beek has been working as a software engineer at Philips TASS since 1999. He worked on a Digital Video Broadcasting system based on COM components. After that he has worked on test and factory equipment for DVD+RW drives. And since the last 2 year he is working on firmware for the Philips/BenQ Blu-ray drive.


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
QuestionAnother possibility PinmemberJames_72219:03 5 Jul '11  
GeneralSingleton flawed PinmemberMark Ginnane15:03 24 Jul '10  
GeneralI want to subclass CMySingleton but I'm having problems... PinmemberDemented-TED19:06 24 Mar '09  
Questionwont compile on vs2005. Pinmembermiche_mannen9:39 26 Sep '07  
Generalimplemetation PinmemberPaul the Great1:33 18 Sep '07  
GeneralSimilar threadsafe (+older) article: PinmemberPaul Evans21:51 9 Nov '03  
GeneralYour sample makes you still have to destroy the Singleton. PinmemberWREY12:01 6 Jul '03  
GeneralRe: You sample makes you still have to destroy the Singleton. PinmemberBrian van der Beek20:34 6 Jul '03  
GeneralRe: Your sample makes you still have to destroy the Singleton. PinmemberWREY0:34 7 Jul '03  
GeneralArticle updated: PinmemberBrian van der Beek20:58 20 Mar '03  
GeneralDoes not fully implement the singleton pattern I'm afraid PinmemberJörgen Sigvardsson13:05 20 Mar '03  
GeneralRe: Does not fully implement the singleton pattern I'm afraid PinmemberJim A. Johnson13:53 20 Mar '03  
GeneralRe: Does not fully implement the singleton pattern I'm afraid PinmemberJörgen Sigvardsson22:24 20 Mar '03  
GeneralRe: Does not fully implement the singleton pattern I'm afraid PinmemberBrian van der Beek20:31 20 Mar '03  
QuestionUhhhhh? Singleton Template? PinsussAnonymous12:37 20 Mar '03  
AnswerRe: Uhhhhh? Singleton Template? PinmemberBrian van der Beek20:14 20 Mar '03  
GeneralRe: Uhhhhh? Singleton Template? PinsussAnonymous8:16 21 Mar '03  
GeneralIf templates are your thing PineditorBarry Lapthorn11:58 20 Mar '03  
GeneralRe: If templates are your thing PinmemberJoão Paulo Figueira13:02 20 Mar '03  
GeneralRe: If templates are your thing PinmemberJörgen Sigvardsson13:11 20 Mar '03  
GeneralRe: If templates are your thing PinmemberPeter Hancock13:35 20 Mar '03  
GeneralRe: If templates are your thing PinmemberBrian van der Beek20:11 20 Mar '03  

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
Web03 | 2.5.120517.1 | Last Updated 20 Mar 2003
Article Copyright 2003 by Brian van der Beek
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid