Essentially, what’s the difference between:
const Date& default_date()
{
static Date dd(2001,Date::Jan,1);
return dd;
}
and
const Date default_date()
{
static Date dd(2001,Date::Jan,1);
return dd;
}
Does the function signature really matter? I don’t think of Date& as a type like *Date, so I’m not sure what difference this makes. Does it just prevent a copy from being made on the return? But then wouldn’t you return &dd?
The first function returns a const reference to the static object, so you can do this:
or
The second one makes a copy of the
static Dateobject, so you can only get a copyReturning
&ddwould return the address of the static object, which could then be assigned to a pointer toDate. The syntax for the return statement is the same for return by reference or return by value. The semantics depend on the return type of the function.Note that in C++, functions such as
default_dateare not referred to as constructors.