Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!

i am new programmer in C++,Please tell me what is the use of Private Constructor in C++,And "how to create object in Private Constructor" in C++,please tell me if any have Good Idea About the private Constructor.
Posted

1 solution

The purpose of making the constructor private is to make it impossible to create an instance of the class directly using the constructor from outside the class.
The instance can only be created from within the class or a friend class.

This is usually used in the Singleton Pattern in order to force the a user of the class to call a special method for instance creation.

Singleton Pattern & its implementation with C++[^]

Singleton in C++: Before and after[^]

When it comes to creating objects in a private constructor, you do it in the same fashion as for a public constructor.
 
Share this answer
 
v3
Comments
nv3 2-Nov-14 3:24am    
My 5.
George Jonsson 2-Nov-14 3:35am    
Thanks for the vote.
(I also saw a little error in the first sentence that I corrected.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900