0

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.

2
  • 3
    It's just a convention. You can write a operator= that returns void (according to the language standard), but it'll confuse people, because we all expect it to return a reference to the left-hand side. Also, not sure if you've figured this out yet, but just to save you the trouble, go read Rule of Three. You're implementing one of the three (five, if targeting C++11 or newer) operations, so you need to implement the other two (four). Commented Mar 7, 2022 at 1:29
  • 1
    @SilvioMayolo I think that over ten years should have been enough to assume C++11 unless specifically stated otherwise. Actually, I would assume C++17 by now, mimimum. We are not doing ourselves a favor using a >20 years old version of the language as baseline still. Commented Mar 7, 2022 at 1:41

2 Answers 2

2

An assignment in C++ returns the object assigned to, and it is good practice to not change this kind of behavior when you are overloading an operator (*). This allows to e.g. check the value assigned, or assigning the same value to multiple objects.

Inside a member function, this is a pointer to the current object. So *this is the current (assigned-to) object, and the return type Vector3D& makes it clear that we are returning by reference, not by value.


(*) You cannot actually emulate the short-circuit evaluation of the || and && operators, which is why you should not normally overload them.

Sign up to request clarification or add additional context in comments.

Comments

2

This (I know its slightly different, I wanted to make the chaining clear);

 Vector3D a;
 Vector3D b;
 Vector3D c;
 c=a=b; <<<<=====

is converted by the compiler to this:

 c.operator=(a.operator=(b));

This only works because operator= returns something that can be used as input to the next operator=.

You ask "why return *this;".

Well, the function has to return a reference to this invoked object, the only thing you have is this. You can't return that (excuse the pun) because its not a reference, it's a pointer, so you have to do *this to actually get the object it points to. The compiler sees you have declared the return type as Vector3D& so it knows to, in fact, return a reference, not the object itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.