0

What is the difference between specifying the return value/type(not sure what to say) as a reference:

CVector& CVector::operator= (const CVector& param)

and as a regular variable:

CVector CVector::operator= (const CVector& param)

Thanks!

  • 1
    By convention, `operator=` should return a reference to `*this` so that you can assign multiple vectors at once, much like fundamental types. `vec1 = vec2 = vec3;`. – François Andrieux May 11 '17 at 17:44
  • @FrançoisAndrieux, thank you and thank you guys for linking the duplicate. Do not know why I did not see when I typed in my question :) –  May 11 '17 at 17:47
  • @FrançoisAndrieux I think it's less about that (you can do the same when `operator=` returns a value) but I think it's more about not making an unnecessary copy every time you invoke the assignment operator. – cdhowie May 11 '17 at 18:58
  • That is to say, `a = b = c` would cause `b` to be copy-assigned from `c`, then return a copy of itself as a temporary, then `a` is copy-assigned from the temporary. Then `a` returns itself by value, creating a second temporary. Finally, both temporaries are destroyed. – cdhowie May 11 '17 at 19:04

0 Answers0