post-increment is operator++(int), pre-increment is operator++().
Depending upon the type to which it is applied, there can be considerable performance issues between the two.
The easiest is pre-increment. Take the current value, add 1, save the new value and return the new value - whatever add 1 means in the context of the type.
post-increment means take a
copy of the current value, add 1, save the new value and return the value of the
copy. Depending upon the type, doing a copy could be significant.
That's why, if there is a choice, pre-increment should always be chosen. It doesn't really matter for a type such as an int, but for custom class it could be - depending upon copy constructor.
Also note that pre-increment should return a ref, whereas post-increment has to return a value.
eg for a simple class where data is a member variable of say type int:
1 2 3 4 5 6 7 8 9 10 11 12
|
SomeValue& SomeValue::operator++() // prefix
{
++data;
return *this;
}
SomeValue SomeValue::operator++(int) // postfix
{
SomeValue result = *this;
++data;
return result;
}
| |