Default Constructors vs Default Parameters? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Default Constructors vs Default Parameters?

Is there a preferred way to set default values to objects in c++? With a default constructor, you would need overloaded constructors for varying numbers of inputs, but with default parameters in a parameterized constructor, there is no need for a default constructor and it seems to get the job done with one constructor.

16th Jan 2019, 7:11 PM
25tunez - avatar
2 Answers
+ 4
Of course there's a preferred way: YOURS. Whichever you like better works, and since the default parameter way works without needing to have a default constructor defined, go for it! I prefer to have mine separate, so if I need to define something separately in the default constructor, I can.
17th Jan 2019, 2:53 AM
Zeke Williams
Zeke Williams - avatar
+ 1
I'd prefer the solution that's more readable and less complex, which is the one using default parameters. At leat if the constructor contains more lines of code it's better because for overloading you'd have to write the same code twice.
16th Jan 2019, 7:56 PM
Aaron Eberhardt
Aaron Eberhardt - avatar