Search code examples
c++prefix-operatorpostfix-operator

return value of prefix and postfix in C++


Why in C++ the prefix return a reference but the postfix return a value?


Solution

  • Because with prefix you modify the object and then return it (so it can be lvalue), and with postfix you return the unchanged object (i.e. a copy) and only then update it (this is of course done by first storing the copy in a temporary, updating the original object, and then returning the temporary by value.)