In the book Programming Principles and Practice Using C++ by Bjarne Stroustrup in section 8.6.2 Global initialization, it is recommended to define default values (e.g. for date in a calendar) as follows:
const Date& default_date()
{
static const Date dd(1970,1,1);
return dd;
}
How does this method compare to simply having a global constant as follows?
static const Date dd(1970,1,1);
The default_date
function is declared with external linkage which means it can be used from any translation unit which have a suitable declaration.
The global variable have internal linkage, and thus can only be used in the translation unit it's defined in.