Class Question

could anyone tell me which one of these is legal/illegal? and what's the difference in each of them?

1
2
3
4
Vector Vector::operator+(Vector v);
Vector Vector::operator+(Vector &v);
Vector &Vector::operator+(Vector v);
Vector &Vector::operator+(Vector &v);
all of those are legal as far as i know.
the first one takes a copy of a vector object, does operations on it, and returns a copy of the result. the second one takes a reference to the actual object and returns a copy of the result. the second one takes a copy of the object and returns a reference to the result. the last one takes a reference to the object and returns a reference of the result. however, i would have personally done this:

Vector& Vector::operator+(const vector &v) const;
They are all technically legal declarations on their own, but only the first one would allow operator+ to be used normally.

The second one won't accept const and rvalue arguments, and the third/fourth ones don't return the result by value.

You could also use Vector Vector::operator+(const Vector &v), or, even better, the non-member Vector operator+(const Vector& lhs, const Vector& rhs) or Vector operator+(Vector lhs, const Vector& rhs)
Topic archived. No new replies allowed.