+ 4
Private constructors
What is the use of Private Constructors in C++ ?
1 Antwort
+ 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.