I've just begun getting into classes in c++ and I was working on a class that defines 3D vectors. I was asked to overload the = operator and my professor suggested to implement it like this:
Vector3D& Vector3D::operator=(const Vector3D &rhs){
vec[0] = rhs[0];
vec[1] = rhs[1];
vec[2] = rhs[2];
return *this
}
I'm confused as to where the reference is returned and why do we use the "This" pointer. I asked my professor about this and he told me that it's necessary when we try to do succesive assignments like this:
Vector3D a;
Vector3D b;
Vector3D c=a=b;
Still, I don't understand why its necessary to have a return value as we have already updated the values before.