Can we have private constructors in c++? If yes, what is the use of it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

Can we have private constructors in c++? If yes, what is the use of it?

4th Jul 2017, 1:34 AM
Hariharasubramanian
4 Answers
+ 9
Thank you all
5th Jul 2017, 2:21 PM
Hariharasubramanian
+ 7
Yes
4th Jul 2017, 1:34 AM
Hariharasubramanian
+ 2
For creating an object of that class within the class itself
4th Jul 2017, 3:25 AM
aklex
aklex - avatar
+ 1
Sample uses (there are others): - Several overloaded public constructors can forward-call a private constructor to implement constructor functionality after doing some type conversions. - A public factory method can call a private constructor to control creation of this class's objects.
4th Jul 2017, 2:21 AM
Igor B
Igor B - avatar