hello, as the tittle says i have a simple question.
Our teacher told us that there is a difference between those ++ and --, when used as prefix or postfix in a code.
so could someone explain me in detail what is the diference and give me an example if possible.
I tried myself to figure out the difference but i got the same result for both for ex. "p++" and "++p".
int& prefix_increment( int& lvalue ) // ++lvalue: return reference
{
// step 1. side effect
lvalue = lvalue + 1 ; // increment the lvalue
// step 2. value computation
return lvalue ; // return reference to the incremented value
}
int postfix_increment( int& lvalue ) // lvalue++: return prvalue
{
// step 1. value computation
int temp = lvalue ; // create a temporary holding a copy of the lvalue (before it is incremented)
// step 2. side effect
prefix_increment(lvalue) ; // increment the lvalue, discard the result
// return the value computed in step 1.
return temp ; // return the temporary (which holds a copy of the original value)
}