the "this" keyword


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


i was read about the "this" keyword i understood that its a pointer to an object, but in this example it is returning a *this what is it returning exactly cause from my understanding of return it can only give back one.
The operator is returning a reference to itself. This allows you to stack assignments:

1
2
3
4
5
6
CVector a, b, c;

a = b;  // the = operator will return a reference to a

// which means that:
c = a = b;  // this is OK, becase "a = b" returns a reference to a, which is then being assigned to c 
Last edited on
Topic archived. No new replies allowed.