0

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. https://code.sololearn.com/cOy2MvoMyzOK/?ref=app

16th Jan 2019, 7:11 PM
25tunez
25tunez - avatar
2 Respostas
+ 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