Can I declare a C++ Constructor in Private? What is Singleton class?

exploreIT
3 min readJul 26, 2022

--

Yeah, for sure! Not all situations require or demand public constructor. You can use it in both public and private as long as you know when and how to use it. So, let’s try to understand the ‘when’ and ‘how’-

The reason we use constructor is- to construct an object and assign values to the object’s members. More formally, it is used to create an object of a class with proper initial state, and in order to create the object of the class anywhere in the code (in any class, any package) we generally make it public. So, by the simple logic you can make a constructor private if you don’t want a class’s objects to be used anywhere in the code (just the opposite one). But the question arises- If we can’t use it, why should we declare then?

Ahh, not actually, you make a constructor private that doesn’t mean it can’t be used. Moreover, there are certain cases when we actually want our constructor to be private. Now, there are a few ways you can declare constructor in private and access them (like- using friend class), but here I’m gonna show you a practical example when we actually require private constructors, which is Singleton Class.

What is Singleton class?

It is basically a design pattern. Sometimes we need to have only one instance of our class. For example, a single DB connection shared by multiple objects, as creating a separate DB connection for every object may be costly. Or, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. Here’s a simple example of a Singleton design pattern (we’ll later discuss how the private constructor is helping us in this scenario)-

PROGRAM:

Fig-1: A very basic example of Singleton class in C++

OUTPUT: (I have attached the code in ‘comment section’, in case you want to execute it)

Fig-2: Output of the above program

EXPLANATION: If you observe, you can see- we are not creating any object by directly calling the class’s constructor (we can’t do that BTW, hence it is in private), what we are doing is- just calling another member function for creating instances, and the interesting thing is- the function internally checks whether any object of the class is already instantiated or not, if it is already there then it returns that one (doesn’t create a new one). Hence we were tried to create a new object twice (by calling ‘getSingleton()’) we get a message saying “One instance is already present”, and also when we print values in both the objects (created by ourselves) we get the same value (Even if one was set to ‘100’ and another ‘999’. This is how a single copy is maintained here throughout the program. (This is a very basic example, but the idea is used in a lot of scenarios)

Hey, did you like the article?

If yes then don’t forget to follow exploreIT as I regularly come up with interesting topics related to programming. And of course, if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!

Feet free to connect — https://www.linkedin.com/in/salman-shaikh-82989b1b9/

See you in the next article, Bye Bye!

Related Article(s)

--

--

exploreIT
exploreIT

Written by exploreIT

This is Salman, a Software Developer. Here in exploreIT, I come up with interesting concepts related to programming that you may enjoy. Do check & have fun:)

Responses (1)