The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference.
Why is it necessary to retain a temporary copy of the original variable ?
Why is it necessary to retain a temporary copy of the original variable ?
It's not necessary if you don't mimic the functionality of the built in ++ and -- operators while overloading them. But if you do you'll find that the postfix operator requires you to temporarily store the old value like
Code:
T T::operator++(int) { // generic postfix ++ overloading
T old(*this); // store old value
// increment *this
return old; // return old value
}
You don't have to store the old value in the prefix counterpart.
The increment refered to in this instance sample is an increment on an iterator class. And this may indeed be slower. when overloading operators. anything can happen. The ++ operator isn't even required to increment anything. It may just as wel be an 'abuse' to achieve some other effect on the class.
On an int it is most likely not to make a difference. Although I have had cases where post incrementing an int yielded a slightly better result than pre incrementing. In fact. I have never had to change a postincrement (on an int) into a preincrement to get better code, while I have had to do the reverse on several occasions. But the cases are rare anyways.
Well, I always prefer preincrement over post increment, unless the program logic dictates otherwise. One reason is that the code is consistent, irrespective of the datatype. And proving a point in C++ by pointing at the assembly code generated by a compiler seems redicilous to me. As then before writing anything one would have to look at the code generated by the compiler, God knows how many C++ compilers exits now (not to mention different versions ). And last but not least the Gurus prefer preincrement over postincrement.
Kind Regards,
Usman.
Well, I always prefer preincrement over post increment...... And last but not least the Gurus prefer preincrement over postincrement.
If it's a choice between
for(int j = 0; j < MAX; j++)
or
for(int j = 0; j < MAX; ++j)
It is merely a matter of style. If your choice is due to the reason quoted here I would like to point out that Bjarne Stroustrup prefers
for(int j = 0; j < MAX; j++)
You can see evidence of this preference in some of his papers.
For (most) non-fundamental types ++i will be more efficient than i++.
You know old habits die hard, and it would be very easy to increment a std::map iterator using post-increment operators if that is what you do all the time. (Men are by nature creatures of habit.) For this reason alone, I have seen many people suggest that the pre-increment operator always be used -- even for fundamental types -- unless there is specific need for the post-increment operator. I find it difficult to argue with such logic.
So, I have looked what the compiler generate as assembly code. Well, it is the same. Independend of using PRE or POST INC. (Microsoft VC++)
But it's real: for Objects it depends on implementation. And like all other say here, NORMALLY you need for POST INC an temporary variable as return.
And for big objects with memory allocation etc. its then better to use PRE INC. to avoid such memory allocations.
Bookmarks