Constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Constructor

is constructor always public?

13th Aug 2017, 10:22 AM
yugandar sooraz
yugandar sooraz - avatar
4 Answers
+ 1
In singleton design pattern , we have constructor as private
13th Aug 2017, 11:23 AM
Vignesh Vicky
Vignesh Vicky - avatar
0
If you want to create object of given class- yes. But sometimes when you do not want to create object, you may make it private. (I wrote a code in which class has private ctor) https://code.sololearn.com/cub4oA2riU4b/?ref=app
13th Aug 2017, 10:29 AM
Jakub Stasiak
Jakub Stasiak - avatar
0
@Vignesh can you give source of such an implementation of Singleton? Everyone and every site I've been learning from told me that ctor in singleton is private.
13th Aug 2017, 9:48 PM
Jakub Stasiak
Jakub Stasiak - avatar
0
sorry @jakub stasiak typo error it is Private.. link : http://www.yolinux.com/TUTORIALS/C++Singleton.html if you want to prevent your object from being copied you have to declare the constructor as private.
14th Aug 2017, 2:26 AM
Vignesh Vicky
Vignesh Vicky - avatar