Private constructors | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 4

Private constructors

What is the use of Private Constructors in C++ ?

15th Feb 2018, 5:09 AM
amir khodayian
amir khodayian - avatar
1 Respuesta
+ 5
Using a private constructor stops other classes from creating an instance of the class using that constructor. For example, you may want a parameterless constructor that initializes all of your variables, but you need more information to be provided to create a valid instance. All other constructors may call the parameterless constructor then continue to initialize the object with the additional information. Private constructors are also used when creating a Singleton class where you want one and only one instance of it, creating by the class itself. You'll probably encounter a few other scenarios where it may seem appropriate when your actually coding.
15th Feb 2018, 5:18 AM
Jesse Bayliss
Jesse Bayliss - avatar