The advantage of writing a useful constructor over, say, this:
1 2 3 4
|
Date d;
d.day = 4;
d.month = 2;
d.year = 42;
| |
is that if you ever wanted to change Date to include something else, say a day of the week, you have
to find all places in the code where you instantiate a Date and fix them. Finding all the places can be
error prone -- it is very easy to miss one, and you'll get no complaint from the compiler, just that
instance isn't fully initialized.
I'd like to make it 100% fool proof. The problem with everid's approach is that it naturally wants me
to make the update by adding a fourth parameter to the constructor and defaulting it too, which means
that my code:
compiles without complaint even though the day of the week is now set to whatever the parameter defaults
to, and I'm back to my original problem.
With my approach, I add a new parameter, and now every place where I instantiate a Date with only three
parameters instead of four is a compile error -- the compile finds all the places in the code I need to update
for me, 100% foolproof. I'm now much less likely to accidentally introduce a bug due to an uninitialized member.