Question

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

Was it helpful?

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
scroll top