I saw an example about implementing pre-increment and post-increment, which claims that overloading pre-increment is able to be defined as
T& T ::operator++()
and overloading post-increment can be defined and implemented in terms of pre-incremet as follows
const T T::operator++(int){
const T old(*this);
++(*this);
return old;
}
I have two questions:
1) what does “old” mean?
2) ++(*this) is assumed to use the pre-increment, and the original pre-increment definition does not have argument. However, it has *this here.
what does "old" mean?
The method is a post increment. The current value ("old value") is returned and then the value is incremented ("new value").
++(*this) is assumed to use the pre-increment, and the original pre-increment definition does not have argument. However, it has *this here.
*this
is not an argument. The parentheses are not necessary, they are there for readability.
It's equivalent to ++*this
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With