Firstly if Date temp = *this, then I dont see why the return type is any different for these two functions?
Let's compare this with the situation of ++
on good old int
. Consider
int i = 1;
int j = i++;
After this, j
holds the old value of i
, but i
itself is incremented. So i
must have been copied prior to the increment, as if ++
on int
were defined as
class int { // pseudocode
public:
int operator++(int)
{
int temp = *this;
*this += 1;
return temp;
}
};
OTOH, after
int i = 1;
int j = ++i;
i
and j
have the same value, so ++
must have been implemented as
int &operator()
{
*this += 1;
return *this;
}
where the change from int
to int&
introduces convenience: there's no need to make a copy and it's possible to use ++i
in a situation where a reference is needed.
Secondly, why does the parameter for the second function not have a variable name?
Because it should never be used. The argument is there as a syntactic gimmick so the compiler can distinguish the two types of operator++
(pre- and post-increment) from each other, but it doesn't have any well-defined value. Giving it a name would trigger an "unused identifier" option in a compiler with proper warnings enabled.