Singleton implementation in Visual Studio C++ sample project

In this post I will create an implementation of the Singleton design pattern in a Visual Studio C++ (2017)  project, my sample has passed the sonar quality check  (it’s the case of all take4make sample).

This is the structure of the c++ project :

NB: thanks to Microsoft for making available the visual studio version 2017 free for learning purpose.

Basically I create Singleton class [header= Singleton.hpp implementation Songleton.cpp ]  and Caller.cpp that use the Singleton class

So to succeed  the C++ Singleton class implementation I need to answer these 2 questions.

How  to instantiate the singleton class only one time during execution life (in other words  how to  make impossible to instantiate more than one instance  )?

How to get access on the alone singleton instance from every where?

So we need to create Singleton class with a private constructor (see Singleton.hpp) so that it will be impossible to instantiate it by other c++ source file. Also we need to create a static attribute of type Singleton MyInstance which will hold the unique instance reference. Last thing we will create static method GetInstance() that check if MyInstance null and then create it using the private constructor. So GetInstance() will  take in charge  checking that we have only an alone instance of Singleton class, and return it. Also it will create if it’s not already created. This will guarantee instantiation in the first call.

Singleton.hpp

/*
* created in scope of www.take4make.com samples
* @author khalil CHARA
*/

#pragma once

/**
* Singleton Class tha show the singleton designe pattern implementation
*/
class Singleton
{
private:
/**
* single instance of Singleton class , it's private to avoid access directly by other classes
*/
static Singleton *MyInstance;
/**
* double number to check unicity of singleton instance
*/
double RandomNumber = 0;
/** private constructor to avoid use by other classes
* the constructor chose random number to fill randomNumber variable,
* this will show us in case of many call of singleton that it's about the same instance.
*/
Singleton();
~Singleton();

public:
/**
* get local and single instance myInstance
* it has static modifier to be the point of access
* this method create the single instance in the first call. see the Caller class to complete the vision
*/
static Singleton *GetInstance();
/**
*Simple getter of randomUmber to see if it's about many instantiation or single instantiation
*/
double GetRandomNumber();
};

 

NB: The random attribute is for test purpose in Caller.cpp to show that only one instance of singleton is created

Singleton.cpp

/*
* created in scope of www.take4make.com samples
* @author khalil CHARA
*/

#include "pch.h"
#include "Singleton.hpp"
#include <stdlib.h>

//constructor implementation
Singleton::Singleton()
{
//generate random number to prove for caller that there is only one instance created
Singleton::RandomNumber = rand();
}

//destructor implementation
Singleton::~Singleton()
{
//put free instruction if you did allocation in singleton class.
}
//get_instance implementation
Singleton *Singleton::GetInstance() {
//check if myInstance already created , otherwise create it and return it for callers.
if (!Singleton::MyInstance) {
Singleton::MyInstance = new Singleton;
}
return Singleton::MyInstance;
}
//get_random_number implementation
double Singleton::GetRandomNumber() {
return Singleton::RandomNumber;
}
// Instantiation t0 = null pointer
Singleton *Singleton::MyInstance = nullptr;

Caller.cpp

/*
* created in scope of www.take4make.com samples
* @author khalil CHARA
*/

#include "pch.h"
#include <iostream>
#include "Singleton.hpp"

int main()
{
Singleton *sngltnT1 = Singleton::GetInstance();
Singleton *sngltnT2 = Singleton::GetInstance();
std::cout << "sngltnT1 number is : " << sngltnT1->GetRandomNumber() << "\n";
std::cout << "sngltnT2 number is : " << sngltnT2->GetRandomNumber() << "\n";
}

// Execute the software : Ctrl+F5
// debug the software : F5

Result of the execution :

sngltnT1 number is : 41

sngltnT2 number is : 41

you can see that it’s about the same instance (for sngltnT1 and sngltnT2) because the random number is the same. if it was about different instances the random number will be different.

Down load the full project :  T4MCppSingleton

Leave a Reply

Your email address will not be published. Required fields are marked *