Click here to Skip to main content
13,589,923 members
Click here to Skip to main content
Add your own
alternative version


77 bookmarked
Posted 4 Mar 2002

Singleton Pattern & its implementation with C++

, 4 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Singleton is one of the commonly used patterns in object oriented developments. In this article I am discussing abt this pattern in general and how we can implement this pattern with C++.


Suppose we have to use a single object of a class throughout the lifetime of an application. In C++, it is possible to declare a global object, which can be used anywhere inside the program. But a good object oriented design strictly prohibits the use of global variables or methods, since they are against the fundamental principles of object orientation like data encapsulation or data hiding. More over, most latest object oriented programming languages like JAVA or C# do not support global variables or functions.

Another practical solution to get a single object is by declaring a class, which contains only static methods. A static class is loaded into memory when the execution of the program starts and it remains there till the application ends. Remember that for invoking a static method of a class, it is not necessary to create an instance of the class. But remember that a class with only static methods and variables are not a good object oriented design. A class of static methods unfortunately breaks down to a list of functions or utilities.

When we want to create only one instance of a class in a truly object oriented fashion by adhering to the basic principles of object oriented programming, the Singleton patterns are used. The Singleton Pattern comes under that classification of Creational Pattern, which deals with the best ways to create objects. The Singleton Design pattern is used, where only one instance of an object is needed throughout the lifetime of an application. The Singleton class is instantiated at the time of first access and same instance is used thereafter till the application quits.

There are very good non-software examples available in real world for Singleton patterns. The office of the Principal of my college is a Singleton. The University specifies the means by which a principal is selected, limits the term of office, and defines the order of succession. As a result, there can be at most one active principal at any given time. Regardless of the personal identity of the principal, the title, "The Principal" is a global point of access that identifies the person in the office.

The Singletons are often used to control access to resources such as database connections or sockets. Suppose we have a license for only one connection for our database. A Singleton connection object makes sure that only one connection can be made at any time.

It is pretty easy to implement the Singleton Pattern in any object oriented programming languages like C++, JAVA or C#. There are lots of different ways to implement the Singleton Pattern. But by using a private constructor and a static method to create and return an instance of the class is a popular way for implementing Singleton Pattern. The UML representation of a Singleton Pattern is shown below.

C++ Implementation

Creational Pattern: SINGLETON
Author: Rajesh V.S
Language: C++

#include <iostream>

using namespace std;

class Singleton
    static bool instanceFlag;
    static Singleton *single;
        //private constructor
    static Singleton* getInstance();
    void method();
        instanceFlag = false;

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
    if(! instanceFlag)
        single = new Singleton();
        instanceFlag = true;
        return single;
        return single;

void Singleton::method()
    cout << "Method of the singleton class" << endl;

int main()
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc2 = Singleton::getInstance();

    return 0;


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

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerRe: Is it really a singleton? Pin
vish11112-Aug-08 22:55
membervish11112-Aug-08 22:55 
AnswerRe: Is it really a singleton? Pin
Member 1211239726-Apr-16 22:03
memberMember 1211239726-Apr-16 22:03 
GeneralAlternative Thread Safe Singleton Pin
Paul Evans8-Dec-02 1:34
memberPaul Evans8-Dec-02 1:34 
QuestionMemory leak ? Pin
GizMoCuz9-Jul-02 11:31
sussGizMoCuz9-Jul-02 11:31 
AnswerRe: Memory leak ? Pin
jan larsen25-Jul-02 2:51
memberjan larsen25-Jul-02 2:51 
GeneralRe: Memory leak ? Pin
Chris Stoy7-Oct-02 4:04
sussChris Stoy7-Oct-02 4:04 
GeneralRe: Memory leak ? Pin
jan larsen7-Oct-02 4:57
memberjan larsen7-Oct-02 4:57 
GeneralRe: Memory leak ? Pin
Paul Evans8-Dec-02 1:40
memberPaul Evans8-Dec-02 1:40 
I've marked a race condition in the code below - the null check is only any good as a lock hint in a multiple threaded app.

Singleton* Singleton::getInstance()<br />
{<br />
if ( 0 == s_singletonInstance )  //Race condition here - possiblilty of 2+ singletons being created if code swapped out after this line<br />
{ s_singletonInstance = new Singleton(); }<br />
return s_singletonInstance;<br />

You expect static variables to be cleaned up by the compiler. Of course static pointers are a whole different kettle of herring! WTF | :WTF:

// Paul Evans, UK.
GeneralRe: Memory leak ? Pin
Anonymous10-Jul-03 12:33
sussAnonymous10-Jul-03 12:33 
GeneralRe: Memory leak ? Pin
Puponacid8-Sep-04 7:52
memberPuponacid8-Sep-04 7:52 
GeneralRe: Memory leak ? Pin
jan larsen8-Sep-04 19:39
memberjan larsen8-Sep-04 19:39 
GeneralRe: Memory leak ? Pin
Puponacid10-Sep-04 1:09
memberPuponacid10-Sep-04 1:09 
GeneralRe: Memory leak ? Pin
Anonymous10-May-05 9:26
sussAnonymous10-May-05 9:26 
GeneralRe: Memory leak ? Pin
jan larsen10-May-05 21:40
memberjan larsen10-May-05 21:40 
GeneralRe: Memory leak ? Pin
Kontaxis25-Jun-08 10:29
memberKontaxis25-Jun-08 10:29 
GeneralRe: Memory leak ? Pin
jan larsen26-Jun-08 3:29
memberjan larsen26-Jun-08 3:29 
GeneralNice, but... Pin
Nish - Native CPian16-Jun-02 22:36
memberNish - Native CPian16-Jun-02 22:36 
Questionusing Mutex? Pin
johnthan16-Apr-02 5:13
memberjohnthan16-Apr-02 5:13 
AnswerRe: using Mutex? Pin
jan larsen25-Jul-02 2:54
memberjan larsen25-Jul-02 2:54 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180618.1 | Last Updated 5 Mar 2002
Article Copyright 2002 by vsrajeshvs
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid