i--
is the post-fix decrement operator. For builtin types it means that i
is decremented by one (1). The value of the expression i--
is the value of i
before decrementing it.
--i
is the pre-fix decrement operator. For builtin types it means that i
is decremented by one (1). The value of the expression --i
is the value of i
after decrementing it.
For the builtin types you cannot change the behaviour of the increment or decrement operators. If you want to have another behaviour, you need to fallback to the operators +=
and -=
where you can add whatever you want. But please note that your code used int
and you probably want to change to float
or double
first before adding or subtracting 0.1
or 0.2
.
You can change the behaviour for increment and decrement operators for your own types, though. Here is an exammple
#include <iostream>
struct S {
S& operator++() { // prefix increment "++s"
d += increment_value;
return *this;
}
S& operator--() { // prefix decrement "--s"
d -= decrement_value;
return *this;
}
S operator++(int) { // postfix increment "s++"
S copy{*this};
d += increment_value;
return copy;
}
S operator--(int) { // postfix decrement "s--"
S copy{*this};
d -= decrement_value;
return copy;
}
double d{};
double increment_value{0.2};
double decrement_value{0.1};
friend std::ostream& operator<<(std::ostream& os, const S& s) {
os << s.d;
return os;
}
};
int main(int, char**) {
S s{1};
std::cout << "val=" << s.d << '\n';
std::cout << "prefix++=" << ++s << '\n';
;
std::cout << "val=" << s.d << '\n';
std::cout << "postfix++=" << s++ << '\n';
std::cout << "val=" << s.d << '\n';
std::cout << "prefix--=" << --s << '\n';
std::cout << "val=" << s.d << '\n';
std::cout << "postfix--=" << s-- << '\n';
std::cout << "val=" << s.d << '\n';
}
The output is
val=1
prefix++=1.2
val=1.2
postfix++=1.2
val=1.4
prefix--=1.3
val=1.3
postfix--=1.3
val=1.2
You might notice that I added two operator ++
and two operator--
. One of each is getting no parameter, the other is getting an int
. As cppreference describes, the int
parameter is only a dummy parameter to differentiate between the prefix and the postfix versions of the operators. (*)
As you can also see, the prefix operators are more performant as they don't need the extra copy. So normally please use the prefix versions (compare the CppCoreGuidelines).
But, please ignore the possibility to change the increment and decrement operators. It is counter intuitive and only a source of errors and head aches. Don't do it this way!
(*) This parameter could also be used for even more confusing behaviour, but it should be clear that this is making the idea even worse. So see this just as an example of what is technically possible.
#include <iostream>
struct S {
S operator++(int diff) { // postfix increment "s++", diff defaults to zero (0)
S copy{*this};
d += diff ? diff : increment_value;
return copy;
}
S operator--(int diff) { // postfix decrement "s--", diff defaults to zero (0)
S copy{*this};
d -= diff ? diff : decrement_value;
return copy;
}
double d{};
double increment_value{0.2};
double decrement_value{0.1};
friend std::ostream& operator<<(std::ostream& os, const S& s) {
os << s.d;
return os;
}
};
int main(int, char**) {
S s{1};
std::cout << "val=" << s.d << '\n';
s++;
std::cout << "val=" << s.d << '\n';
s.operator++(2);
std::cout << "val=" << s.d << '\n';
s--;
std::cout << "val=" << s.d << '\n';
s.operator--(2);
std::cout << "val=" << s.d << '\n';
}
results in
val=1
val=1.2
val=3.2
val=3.1
val=1.1