The preincrement (++i
) does the increment and then return a reference to the variable (Wich have been modified). Postincrement (i++) computes the result of the increment, stores it in a temp local variable, and return a copy of that result after it does the increment. This is done to make look that the increment is done after the call:
int operator++(int)
{
int tmp( *this );
++(*this);
return tmp;
}
This code is for learning purposes, is not real code (int is not a class). Is for showing how postincrement works.
So, as you can see, i++
returns a copy, not a reference. So you cannot initialize the return value, because its a rvalue, not a reference to an existing variable.