-1

I am new in C++ and I've got some questions. I saw operator ++ overloading at complex numbers and I cant understand:

  1. Why I create a tmp variable
  2. why my first operator is Complex & and second it's just Complex
  3. Why return *this;(i know cause of Complex& but why)
  4. if i used Complex without & what would happened?(i run it and its the same result)

Don't look at comments it's Greek :)

Complex &   Complex::operator ++(){
     r=r+1;
     return *this;//to this einai to pointer tou trexontos alla 8eloume dereference ara vazoume *this gia na epistrepsoume refernce//
}

Complex     Complex::operator ++(int ){
     Complex tmp(*this);
     operator ++();
        return tmp;//ftiaxnoume ena tmp tou antikimenou pou exoume meta efarmozoume to operator++ kai epistrefoume to tmp//
}
Panos
  • 77
  • 1
  • 7
  • See http://stackoverflow.com/a/4445969/78845 for an explanation of pre- vs post-increment operators in C++. – johnsyweb Nov 24 '14 at 22:51
  • possible duplicate of [Post-increment and Pre-increment concept?](http://stackoverflow.com/questions/4445706/post-increment-and-pre-increment-concept) – johnsyweb Nov 24 '14 at 23:00

3 Answers3

1

the different signature (Complex& vs Complex) is because these operations i++ and ++i do different things. One returns a copy of the old value (the second one) by value, the first one returns a reference to the actual value if x - by reference

The tmp variable is needed because you need to return the old value after incrementing the value (thats what x++ does)

you need to return a reference to the current object - so return *this - this is a pointer to the current object; *this is the object

pm100
  • 48,078
  • 23
  • 82
  • 145
1

2 - Complex & returns a reference to the same object, Complex returns a copy of it.

1 - that is why a temp variable is created, in order to increment the actual object but return the copy, in order to simulate the "use and then increment" postfix operator.

3 - because while i++ returns a copy of i (and increments i), ++i returns i incremented.

4 - the operator will return a copy of the incremented object, not the actual incremented object.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • if i used complex without & what would be happened? – Panos Nov 24 '14 at 23:21
  • 2
    @user3135218 - in this particular case nothing bad. The `++` operator will return a copy of the incremented object instead of the incremented object. But when you do operator overloading, you really should follow the proper convention for return types and parameters. – dtech Nov 24 '14 at 23:30
0

With a variable x, you can write ++x or x++. Former will do a ++, and if it is part of a bigger expression, use the new x. Latter will do the ++ too, but uses the old x for the current expression (it´s like the ++ comes a line after the current one).

If you overload ++, you need to separate if it is prefixed or suffixed.
It looks somewhat strange, but if you mean the suffixed version, you need to have a nameless int as parameter of the function (nothing for prefix). There is no meaningful value passed or something,
it´s just to make a difference between the two possibilites.

So, in the suffix variant, you´ll have a temporary copy to keep
the old value while you´re doing the ++ (ant return the old value for this one time).

deviantfan
  • 11,268
  • 3
  • 32
  • 49