Implementation of Singleton Class in C++
Last Updated :
08 Nov, 2022
A singleton class is a special type of class in object-oriented programming which can have only one object or instance at a time. In other words, we can instantiate only one instance of the singleton class. The new variable also points to the initial instance created if we attempt to instantiate the Singleton class after the first time. This is implemented by using the core concepts of object-oriented programming namely access modifiers, constructors & static methods.
Steps to Implement Singleton Class in C++:
- Make all the constructors of the class private.
- Delete the copy constructor of the class.
- Make a private static pointer that can point to the same class object (singleton class).
- Make a public static method that returns the pointer to the same class object (singleton class).
Below is the implementation of the singleton class in C++:
C++
#include <bits/stdc++.h>
using namespace std;
class Singleton{
private :
string name, loves;
static Singleton*
instancePtr;
Singleton()
{
}
public :
Singleton( const Singleton& obj)
= delete ;
static Singleton* getInstance()
{
if (instancePtr == NULL)
{
instancePtr = new Singleton();
return instancePtr;
}
else
{
return instancePtr;
}
}
void setValues(string name,
string loves)
{
this ->name = name;
this ->loves = loves;
}
void print()
{
cout << name << " Loves " <<
loves << "." << endl;
}
};
Singleton* Singleton ::instancePtr = NULL;
int main()
{
Singleton* GeeksForGeeks
= Singleton ::getInstance();
GeeksForGeeks->setValues( "Manish" ,
"GeeksForGeeks" );
GeeksForGeeks ->print();
cout << "Address of GeeksForGeeks: " <<
GeeksForGeeks << endl;
cout << endl;
Singleton* gfg = Singleton ::getInstance();
gfg->setValues( "Vartika" ,
"GeeksForGeeks" );
gfg->print();
cout << "Address of gfg: " << gfg << endl;
return 0;
}
|
Output
Manish Loves GeeksForGeeks.
Address of GeeksForGeeks: 0x1793010
Vartika Loves GeeksForGeeks.
Address of gfg: 0x1793010
Explanation:
- Firstly, we made all the constructor private so that an instance of the Singleton class can’t be instantiated from outside of it.
- We deleted copy constructor so that copy of the instance cannot be created.
- Created a static member instancePtr and initialized it with NULL. It points to the instance of Singleton class.
- Created a getInstance() method which returns an instance of the Singleton class. It is a static method because static variables are accessed by only static methods and we have to access instancePtr which is a static member.
- If there already exists an instance of the Singleton class then getInstance() will return a pointer to that instance as we can have only one instance of the Singleton class.
- If instancePtr == NULL that means there exists no instance of the Singleton class. So, getInstance() will instantiate an instance of the Singleton class and return a pointer to it.
- We cannot create an instance of the Singleton class as all constructors are private. We have to use the getInstance() method to get an instance of it.
Case 1: An instance of the Singleton Class is created beforehand.
In this implementation, we are creating an instance of the Singleton class beforehand (i.e. initializing instancePtr with an instance instead of NULL using a new keyword) and returning it when getInstance() is invoked.
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Singleton{
private :
string name, loves;
static Singleton *instancePtr;
Singleton()
{
}
public :
Singleton( const Singleton &obj) = delete ;
static Singleton *getInstance()
{
return instancePtr;
}
void setValues(string name,
string loves)
{
this ->name = name;
this ->loves = loves;
}
void print()
{
cout << name << " Loves " <<
loves << "." << endl;
}
};
Singleton *Singleton ::instancePtr
= new Singleton();
int main()
{
Singleton *gfg
= Singleton::getInstance();
gfg->setValues( "Learner" ,
"GeeksForGeeks" );
gfg->print();
cout << "Address of gfg : " <<
gfg << endl;
cout << endl;
Singleton *geeksForGeeks
= Singleton::getInstance();
geeksForGeeks->setValues( "Everyone" ,
"GeeksForGeeks" );
geeksForGeeks->print();
cout << "Address of geeksForGeeks : " <<
geeksForGeeks << endl;
return 0;
}
|
Output
Learner Loves GeeksForGeeks.
Address of gfg : 0xd63010
Everyone Loves GeeksForGeeks.
Address of geeksForGeeks : 0xd63010
Explanation:
- Firstly, we made all the constructor private so that an instance of the Singleton class can’t be instantiated from outside of it.
- We deleted copy constructor so that copy of the instance cannot be created.
- Created a static member instancePtr and initialized it with an instance using the new keyword. It is pointing to the instance of the Singleton class.
- Created a getInstance() method which returns instancePtr of the Singleton class. It is a static method because static variables are accessed by only static methods and we have to access instancePtr which is a static member.
- We cannot create an instance of the Singleton class as all constructors are private. We have to use the getInstance() method to get an instance of it.
Case 2: When the instance is created without using the getInstance() method to create the Singleton Class.
Below is the C++ program to implement the singleton class without using getinstance() method:
C++
#include<bits/stdc++.h>
using namespace std;
class Singleton{
private :
string name, loves;
static Singleton *instancePtr;
Singleton()
{
}
Singleton(string name,
string loves)
{
this ->name = name;
this ->loves = loves;
}
public :
Singleton( const Singleton &obj) = delete ;
static Singleton *getInstance()
{
if (instancePtr == NULL)
{
instancePtr = new Singleton();
return instancePtr;
}
else
{
return instancePtr;
}
}
void setValues(string name,
string loves)
{
this ->name = name;
this ->loves = loves;
}
void print()
{
cout << name << " Loves " <<
loves << "." << endl;
}
};
Singleton *Singleton ::instancePtr = NULL;
int main()
{
Singleton *geeksForGeeks
= new Singleton();
return 0;
}
|
Output
Explanation: As shown in the above output we are not able to instantiate an instance of the Singleton class by using the constructor. We can have only one instance of the Singleton class which we will get by using the getInstance() method.
Share your thoughts in the comments
Please Login to comment...