7

I have these code of lines from a program my teacher made :

 TimeKeeper& operator++() {
        d_seconds++;
        return *this;
  }
  const TimeKeeper operator++(int) {
        TimeKeeper tk(*this);
        ++(*this);
        return tk;
  }

And one of the questions my teacher asked us was "operator++() returns a reference and operator++ (int) returns a value, explain why?"

Can anyone explain this to me?? If you need the rest of the code i dont mind putting it on! Thanks!!

user2843560
  • 115
  • 4

3 Answers3

7

The one without an extra int is the preincrement operator while the one with the extra int parameter is the post increment operator. This somewhat awkward notation is somewhat of a hack to distinguish the two notations and the int can't be used for any useful purpose:

TimeKeeper keeper;
++keeper; // pre increment: calls TimeKeeper::operator++()
keeper++; // post increment: calls TimeKeeper::operator++(int)

The difference between pre increment and post increment is that for pre increment the value of the expression is that after the increment while for post increment it is the value before the expression. For post increment the object the increment is applied to gets moved forward and a different object representing the state before the increment is returned. The object representing the previous state is a temporary object which only lives within the expression and which, thus, needs to be returned by value. For the pre increment only one value is involved and that can be returned immediately by reference.

In the above snippet the result from keeper++ isn't used: you should only ever use the post increment operator when using its result. Otherwise it will just waste creating a temporary object pretty much along the lines of your teacher's code which is then thrown away. Even if the construction is cheap, it may waste a couple of CPU cycles. The same overloading and reasoning for not using it unless necessary applies to the decrement operator operator--(). Oddly enough, C++ is thus not idiomatic C++!

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
6

operator++() is the preincrement operator (++x), while operator++(int) is the post increment operator (x++). If you understand how these operations work, then you should be able to explain why one of them must return a copy.

Take a simple example:

int x = 1;

std::cout << x++ << "\n";  // prints 1
std::cout << ++x << "\n";  // prints 3

What happened to 2?

The value of x became 2 in the x++ expression, 1 was printed (the value of x prior to the increment operation). Because cout prints the value after the 'real' value was already incremented, it needs a temporary copy of that value.

In the second statement, the value of x becomes 3 in the ++x expression. Because the value has already been incremented, there is no need to copy it and can be used directly, and a reference is returned.

On simple, built in types like int this doesn't matter too much, but copy operations can become expensive when using large classes, especially with big collections.

FalcoGer
  • 2,278
  • 1
  • 12
  • 34
Chad
  • 18,706
  • 4
  • 46
  • 63
0

To distinguish these two operators there must be some syntaxical difference. So to distinguish postincrement operator from preincrement operator a parameter of type int that is not used was added to declaration of postincrement operator. The preincrement operator returns the object itself. It is why its return type a refernce. The postincrement operator returns a temporary object and increment the original object. it is way it returns a value.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335