I agree with @dutch, I don't see Stroustrup said anything about it being better, but he shows an evolution of thought about preference along the way of dealing with default values.
In the book's example there's one point not illustrated in your example, @Xanadu4ever, where there are limits placed on the parameters (a month can't exceed 12, a day should be qualified by the month (and year), etc).
That's not much of an impact itself, and doesn't change your question, but it does clarify that one point where the parameters to the various constructors may require checking or limitation, which he illustrates by making a "Month" type enumeration (instead of an int).
He "evolves" the example of the default constructor for "Date" by first showing the default parameters as part of the constructor with:
Date::Date() : y{2001}, m{Month:jan}, d{1}{}
Then expresses and alternative matching your first form, placing the defaults within the class declaration.
He then illustrates how this can apply to multiple constructors (and I think this is key to the evolution he's following), such that a partial specification is possible, like:
Date::Date(int y) :y{yy}{}
Though he checks for validity, he's pointing out that the defaults for month and day would be from the defaults while the year is provided from the user (and, obviously, the other permutations might apply).
Then he introduces the second form with:
1 2 3 4 5
|
const Date& default_date()
{
static Date dd{2001, Month:jan, 1};
return dd;
}
|
...and then his commentary strikes a chord in my mind:
We used a static...that is created only once....initialized the first time default_date() is called |
To me, this is key. He's avoiding a global value. Consider the case of dynamic library code where it is key to be able to initialize once after the process is running, as in
lazy initialization when the default code is called.
Although he doesn't explicate this is his intention (I'm not sure the subject of dynamic library code is even discussed), it is more generically associated with the avoidance of global variables (as defaults), which he merely references problematic.
Also, he does introduce the second form with a clause about preference, "If we didn't like to build the default value right into the constructor code"...which then leads to this second form as the avoidance of global variables (and their associated initialization problems).
This leads to other examples where this static default value is
accessible to user code as well as the constructor permutations.