Programming Tutorials
Singleton C++
Table of Contents
The Singleton C++ Design Pattern is a C++ design that creates one single instance of a class that can be used throughout multiple files and large projects.
Think of C++ Singleton like a global class that can be accessed at any moment and will be created only once, if it hasn't already been initialized.
It's a very simple concept, that is used so that you can have access to a set of variables in a class without ever initializing the class.
C++ Singleton Example
For example, a good use of singleton would be for logging. You don't want to have to make new instances of your Log class which records errors and logs to a file.
There is a tutorial on how to build a C++ log file class at ID.
You can call the log class like this:
Log::Inst()->Logging("Warning: Just a log!");
You can also initialize it using #define:
#define LOG Log::Inst("error.log")->Logging int main(){ LOG("This is a line of log"); return 0; }
See, notice how there's no initializing (like Log* log = new Log(filename); or Log log(filename);). Using the define you can have it behave like a global function.
C++ Singleton Header File
The following code will show you exactly how to program this neat C++ technique:
#include <string> using namespace std; class Log { public: static Log* Inst(char* filename); void Logging(string message); void Initialize(); ~Log(); protected: Log(); // constructor private: static Log* pInstance; };
We declare an Inst() function, which will return our static Log class pointer. We have an instance called pInstance, which is our static pointer to our class. As well as the constructor and other functions.
C++ Singleton File
The cpp file (C++ file) below:
Log* Log::pInstance = NULL; Log* Log::Inst(char* filename){ if(pInstance == NULL){ pInstance = new Log(filename); } return pInstance; } Log::Log(){ // constructor Initialize(); // you can ignore this function } Log::~Log(){ if(pInstance != 0)delete pInstance; }
I declare the instance as a null pointer. Then I define our Inst() function, which will create a new instance of Log if it isn't created yet.
Make sure you delete pInstance of course.
Important Note:
Since the constructor is protected, you cannot create instances without using the Log::Inst() method.
Major Flaw
Important Note: You have to delete pInstance; at the end of your program otherwise it is a Memory Leak! But if you use a multi-threaded application then this method is your only solution.
Flawed Solution
If your application is single-threaded then this is the perfect solution, but if it is multi-threaded this may not work, since the new operator is thread-safe and this method is not:
Log* Log::Inst(){ static Log inst; return &inst; }
Also you can delete all the pInstance variables from class declaration and implementation.
This solution will work great with any single threaded application as static variables are destroyed automatically if your application terminates by your operating system.
Other Implementations for C++ Singleton
This is also very useful in PHP, not just C++, where you may have only one instance of a database connection (having multiple ones can cause conflict!).


Post new comment