skydoor
skydoor

Reputation: 25898

return value of prefix and postfix in C++

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

Upvotes: 1

Views: 1891

Answers (1)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84189

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.)

Upvotes: 4

Related Questions