Tutorial Overload Operators
May 19, 2010 at 3:27pm UTC
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!
May 19, 2010 at 3:40pm UTC
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.