return value of prefix and postfix in C++
-
22-09-2019 - |
Question
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.)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow