Tutorial Overload Operators

This Overload Operator function works:

1
2
3
4
5
6
CVector& CVector::operator= (const CVector& param)
{
  x=param.x;
  y=param.y;
  return *this;
}


I tried to use the THIS function also with "opterator-", but it didn't work at all. There are a bit too much indirections included for me I think.

Can anyone explain me, HOW the "bold" things above work / why f.e. the "const" cannot be removed (-> Error).

..and how this must look like, that it works:

1
2
3
4
5
CVector& CVector::operator- (const CVector& param){
   x -= param.x;                                             
   y -= param.y;
   return *this;
}


Sorry... but I want to have a good description of this before continuing..

Thanks a lot!
closed account (Lv0f92yv)
CVector& has the ampersand included because you are returning a reference to 'this' (the object itself). The ampersand means 'address of'. You are returning the address of the thing you have just changed.

Same goes for the second example.

Topic archived. No new replies allowed.