Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?

前端 未结 7 1703
情深已故
情深已故 2020-12-02 20:52

I\'m well aware that in C++

int someValue = i++;
array[i++] = otherValue;

has different effect compared to

int someValue =          


        
相关标签:
7条回答
  • 2020-12-02 21:40

    There is one reason, and it has to do with overloaded operators. In an overloaded postincrement function, the function must remember the previous value of the object, increment it, and then return the previous value. In a preincrement function, the function can simply increment the object and then return a reference to itself (its new value).

    In the case of an integer, the above probably won't apply because the compiler knows the context in which the increment is being done, and will generate appropriate increment code in either case.

    0 讨论(0)
提交回复
热议问题