Why use a singleton class?
This design pattern and methodology ensures that only one instance of the C++ class is instantiated. It assures that only one object is created and no more.
It is often used for a logging class so only one object has access to log files, or when there is a single resource, where there should only be a single object in charge of accessing the single resource.
The singleton pattern discussed here gives the class itself, the responsibility of enforcement of the guarantee that only one instance of the class will be allowed to be generated.
Example of a C++ Singleton class:
File:
logger.hpp
05 | static Logger* Instance(); |
06 | bool openLogFile(std::string logFile); |
07 | void writeToLogFile(); |
12 | Logger(Logger const &){}; |
13 | Logger& operator=(Logger const &){}; |
14 | static Logger* m_pInstance; |
Note:
- That the instance function returns a pointer to a static variable and thus is declared static.
- Only the class function Instance can call the constructor. Public
access to the constructor is denied.
- The constructor, copy constructor and assignment operator are all private
to ensure that the programmer using the singleton class can only create a
single instance of the class using only the Instance() function.
- The life of the singleton instantiation is for the duration of the application.
File:
logger.cpp
01 | #include <stddef.h> // defines NULL |
05 | Logger* Logger::m_pInstance = NULL; |
12 | Logger* Logger::Instance() |
15 | m_pInstance = new Logger; |
20 | bool Logger::openLogFile(std::string _logFile) |
Usage:
4 | Logger::Instance()->openLogFile( "logFile.txt" ); |
C++ Singleton class using inheritance:
In this example the base class enforces a singleton pattern.
File:
base.hpp
06 | static sBase* instance(); |
08 | inline int getDataX(){ return mDataX; }; |
09 | inline int setDataX( int _in){ mDataX = _in; }; |
10 | virtual int getDataY() = 0; |
11 | virtual int setDataY( int _in) = 0; |
15 | static sBase* mpoSssInstance; |
File:
base.cpp
01 | #include <stddef.h> // defines NULL |
07 | sBase* sBase::mpoSssInstance = 0; |
09 | sBase::sBase( int _initialValueX) |
10 | : mDataX(_initialValueX) |
16 | return (mpoSssInstance != NULL); |
19 | sBase* sBase::instance() |
21 | if (mpoSssInstance == 0) std::cout << "Class has not been created" << std::endl; |
23 | return mpoSssInstance; |
File:
derived.hpp
05 | class sDerived : public sBase |
08 | static void create( int , int ); |
09 | virtual inline int getDataY(){ return mDataY; }; |
10 | virtual inline int setDataY( int _in){ mDataY = _in; }; |
13 | virtual ~sDerived() {}; |
File:
derived.cpp
06 | sDerived::sDerived( int _initialValueX, int _initialValueY) |
07 | : sBase(_initialValueX) |
09 | mDataY =_initialValueY; |
12 | void sDerived::create( int _initialValueX, int _initialValueY) |
15 | std::cout << "Singleton has already been created" << std::endl; |
17 | mpoSssInstance = new sDerived(_initialValueX, _initialValueY); |
File:
main.cpp
10 | sDerived::create(3,3); |
12 | sDerived::instance()->setDataX(5); |
13 | cout << sDerived::instance()->getDataX() << endl; |
15 | sDerived::instance()->setDataY(7); |
16 | cout << sDerived::instance()->getDataY() << endl; |
Compile:
g++ main.cpp derived.cpp base.cpp
Results:
a.out
5
7
Note that the function calls are static calls to their global names using the scope resolution operator.
The functions create() and instance() are defined as static functions in the class definitions.
Note that static member functions do not have a this pointer as they exist independent of any objects of a class.
The Template Singleton class:
One can inherit a singleton class from a base class or use a template for
each type of instantiation. The Base class and Derived class relationship
offers flexibility as there are no design limits in the derived class.
The Template singleton is also flexible and is used in a manner where any class can be turned into a singleton by using this template.
File:
singleton.hpp
01 | #ifndef __SINGLETON_HPP_ |
02 | #define __SINGLETON_HPP_ |
03 | #include <stddef.h> // defines NULL |
09 | static T* Instance() { |
10 | if (!m_pInstance) m_pInstance = new T; |
11 | assert (m_pInstance != NULL); |
18 | Singleton(Singleton const &); |
19 | Singleton& operator=(Singleton const &); |
20 | static T* m_pInstance; |
23 | template < class T> T* Singleton<T>::m_pInstance=NULL; |
Usage:
01 | #include "singleton.hpp" |
08 | bool openLogFile(string); |
09 | void writeToLogFile(string); |
10 | bool closeLogFile(string); |
16 | bool Logger::openLogFile(std::string) |
25 | typedef Singleton<Logger> LoggerSingleton; |
31 | LoggerSingleton::Instance()->openLogFile( "logFile.txt" ); |
The static stack variable Singleton class:
03 | static Logger& Instance() { |
04 | static Logger theLogger; |
07 | bool openLogFile(string logFile); |
08 | void writeToLogFile(string sLine); |
09 | bool closeLogFile(string logFile); |
13 | Logger(Logger const &); |
14 | Logger& operator=(Logger const &); |
Note:
- This version of a singleton class initializes when the Instance() function is called.
- The Instance() function returns an instance instead of a pointer.
The pointer is not exposed and thus a delete can not be inappropriately applied.
- [Potential Pitfall]:
This form of the singleton can present a problem because of the life expectancy of the object.
If one singleton is instantiated within another, one must be keenly aware of the destructor call sequence.
Usage:
4 | Logger::Instance().openLogFile( "logFile.txt" ); |
Also see An Exception Correct C++ Singleton Template Base Class with Controlled Destruction Order
Other variations and tips:
Optimization option:
1 | static Abc* Abc::Instance() |
3 | return mInstance ? mInstance : (mInstance = new Abc); |
This should slightly reduce the number of computed operations and thus is slightly more optimized.
The examples given above use a C++ global static variable.
Other variations of a singleton can use:
- an environment variable.
- a file (A lock file is often used to insure a single instance of a process).
- a static STL list or STL map of values or paired values can be used by a singleton class as a singleton registry for other classes to verify singleton compliance.

Books: