Open In App

Implementation of Singleton Class in C++

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++:



  1. Make all the constructors of the class private.
  2. Delete the copy constructor of the class.
  3. Make a private static pointer that can point to the same class object (singleton class).
  4. 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++:




// Implementation of Singleton Class
// in C++
#include <bits/stdc++.h>
using namespace std;
 
class Singleton{
  private:
   
  // member variables
  string name, loves;
     
  static Singleton*
         
  // static pointer which will points
  //to the instance of this class
  instancePtr;
   
  // Default constructor
  Singleton()
  {
  }
   
  public:
   
  // deleting copy constructor
  Singleton(const Singleton& obj)
    = delete;
 
  /*
    getInstance() is a static method that returns an
    instance when it is invoked. It returns the same
    instance if it is invoked more than once as an instance
    of Singleton class is already created. It is static
    because we have to invoke this method without any object
    of Singleton class and static method can be invoked
    without object of class
 
    As constructor is private so we cannot create object of
    Singleton class without a static method as they can be
    called without objects. We have to create an instance of
    this Singleton class by using getInstance() method.
  */
  static Singleton* getInstance()
  {
    // If there is no instance of class
    // then we can create an instance.
    if (instancePtr == NULL)
    {
      // We can access private members
      // within the class.
      instancePtr = new Singleton();
       
      // returning the instance pointer
      return instancePtr;
    }
    else
    {
      // if instancePtr != NULL that means
      // the class already have an instance.
      // So, we are returning that instance
      // and not creating new one.
      return instancePtr;
    }
  }
 
  // sets values of member variables.
  void setValues(string name,
                 string loves)
  {
    this->name = name;
    this->loves = loves;
  }
   
  // prints values of member variables
  void print()
  {
    cout << name << " Loves " <<
            loves << "." << endl;
  }
};
 
// initializing instancePtr with NULL
Singleton* Singleton ::instancePtr = NULL;
 
// Driver code
int main()
{
  Singleton* GeeksForGeeks
      = Singleton ::getInstance();
   
  // setting values of member variables.
  GeeksForGeeks->setValues("Manish",
                           "GeeksForGeeks");
   
  // printing values of member variables.
  GeeksForGeeks ->print();
   
  cout << "Address of GeeksForGeeks: " <<
           GeeksForGeeks << endl;
 
  cout << endl;
 
  Singleton* gfg = Singleton ::getInstance();
   
  // setting values of member variables.
  gfg->setValues("Vartika",
                 "GeeksForGeeks");
   
  // Printing values of member variables.
  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:

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++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
class Singleton{
  private:
   
  // member variables
  string name, loves;           
   
  // initializing instancePtr with an
  // instance(outside of this class we
  // are initializing instancePtr with
  // an object.)
  static Singleton *instancePtr;
 
  // Default constructor
  Singleton()
  {
  }
   
  public:
   
  // deleting copy constructor.
  Singleton(const Singleton &obj) = delete;
     
  // returns instancePtr and instancePtr
  // is pointing to an instance of
  // Singleton class
  static Singleton *getInstance()
  {
    return instancePtr;  
  }
 
  void setValues(string name,
                 string loves)
  {
    this->name = name;
    this->loves = loves;
  }
 
  void print()
  {
    cout << name << " Loves " <<
            loves << "." << endl;
  }
};
 
 
// initializing instancePtr with an instance
Singleton *Singleton ::instancePtr
    = new Singleton();
 
// Driver code
int main()
{
  Singleton *gfg
     = Singleton::getInstance();
  gfg->setValues("Learner",
                 "GeeksForGeeks");
  gfg->print();
  cout << "Address of gfg : " <<
           gfg << endl;
 
  // for output indentation
  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:

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++ program to create singleton class
// without using getinstance() method
#include<bits/stdc++.h>
using namespace std;
 
class Singleton{
  private:
   
  // member variables
  string name, loves;
   
  // static pointer which points
  // to the instance of this class.
  static Singleton *instancePtr;
 
  // Default constructor
  Singleton()
  {
 
  }
     
  //same as above
  Singleton(string name,
            string loves)
  {
    this->name = name;
    this->loves = loves;
  }
   
  public:
   
  // deleting copy constructor.
  Singleton(const Singleton &obj) = delete;
 
  static Singleton *getInstance()
  {
    // If there is no instance of class
    // then we can create an instance.
    if(instancePtr == NULL)
    {
      // We can access private members
      // within the class.
      instancePtr = new Singleton();
       
      // returning the instance pointer.
      return instancePtr;
    }
     
    else
    {  
      // if instancePtr != NULL that means
      // the class already has an instance.
      // So, we are returning that instance
      // and not creating new one.
      return instancePtr;
    }
  }
   
  // sets values of member variables.
  void setValues(string name,
                 string loves)
  {
    this->name = name;
    this->loves = loves;
  }
   
  // prints values of member variables.
  void print()
  {
    cout << name << " Loves " <<
            loves << "." << endl;
  }
};
 
// initializing instancePtr with NULL
Singleton *Singleton ::instancePtr = NULL;
 
// Driver code
int main()
{   
  // Gives error
  Singleton *geeksForGeeks
     = new Singleton();
       
  // Cannot create object of Singleton class
  // as default constructor is private &
  // no method is used to access it.
  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.


Article Tags :
C++