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
2 Respuestas
+ 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.
+ 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.