Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading pre-increment and post-increment

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.

like image 376
user785099 Avatar asked Oct 13 '11 01:10

user785099


1 Answers

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.

like image 188
Marlon Avatar answered Sep 29 '22 03:09

Marlon