Singleton Class in C++

Singleton class ensures that there will be only one object of the class in lifetime of a program. Normally we can create as many objects as we want from a particular class. But you’ll be able to create at most one object from a singleton class.

Where to Use Singleton Class?

There are situations where creating multiple instances of a class is not a good idea. It might create problem also.

Example 1: Say, you want to create a HTTP server (web server) and you have a class that gives this functionality. The HTTP server class, say, opens socket, binds to the server IP address and Port, listens for new connection etc. Now you can not create multiple instances of such class. Because your server identity (IP address and Port) is one. If you create multiple objects, every object will try to create socket and bind to same IP address and Port combination. It will create problem. You can be disciplined enough not to create multiple objects. Or, you can design the class such a way that multiple object creation won’t be possible.

Example 2: Say, you have a logger class that logs yours program messages to a file. If you have multiple instances of such class, then the logs can be distributed to multiple files. That won’t be convenient. Better to have only one logger instance.

Example 3: Say, you want to have a connection to a remote database from your program. Having multiple connections to a single database is not a good idea in terms of efficiency. Here also you want to have a singleton class.

If you develop real life application, you’ll find many other situations where you’ll need singleton class.

How to Design Singleton Class

To make a C++ class singleton, you have to do the following things.

  • Make the constructor private to protect object creation in normal way.
  • Make the copy constructor private to protect object copy.
  • Make the assignment operator private to protect object duplication by assignment.
  • Have a self referencing (same type of the class) static pointer to hold the object.
  • Have a static function to create (if not already created) and get the object.

Implementation

Here is the implementation.

class Singleton {
public:
  static Singleton * Instance();
    
  void SetVal(int val);
  void PrintVal();
  
private:
  Singleton() {} // Constructor is private.
  Singleton(Singleton const&) {} // Copy constructor is private.
  Singleton& operator=(Singleton const&) { return *this; } // Assignment operator is private.
  
  static Singleton * instance_;
  
  int val_;
};
 
Singleton* Singleton::instance_ = NULL;

Singleton* Singleton::Instance() {
  if(!instance_) {
    instance_ = new Singleton();
  }
  
  return instance_;
}

void Singleton::SetVal(int val) {
  val_ = val;
}

void Singleton::PrintVal() {
  std::cout << "val = " << val_ << std::endl;
}

Lets consider the program below.

int main(){
  Singleton *p = Singleton::Instance();
  Singleton *q = Singleton::Instance();

  p->SetVal(7);
  
  std::cout << "Prtinting using p: "; p->PrintVal();
  std::cout << "Prtinting using q: "; q->PrintVal();
  
  q->SetVal(9);
  
  std::cout << "Prtinting using p: "; p->PrintVal();
  std::cout << "Prtinting using q: "; q->PrintVal();
  
  return 0;
}

Output of this program is like this.

Prtinting using p: val = 7
Prtinting using q: val = 7
Prtinting using p: val = 9
Prtinting using q: val = 9

In the program above we have two pointers, p and q, of type, Singleton. We set the member value of the class to 7 using pointer p. Then we printed that value using both pointers, p and q. We got the same value 7. That suggests that both p and q points to the same object.

Then we set the member value to 9 using the other pointer q. When we printed the value using p and q, we again got the same value 9. That suggests that the program has only one object of the class Singleton.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

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

0
1
0
0
0
1