Operators should be overloaded only when you are not changing the meaning of those operators.*
What that means is, for example, if there is no mathematical definition of the product of two objects, don't overload the multiplication operator for those objects. If there was a mathematical correspondence, operators overloading can make a class more convenient to use by allowing equations to be expressed in the form a*b + c rather than the more verbose (a.multiply(b)).add(c) Where addition and multiplication operators are used, addition and multiplication should be the intent. Any other meaning is more than likely to confuse others. Some other types where overloading operators is acceptable (and, in my opinion, preferable) are smart pointers, iterators, complex numbers, vectors, and bignums.
This follows from one of the design goals of C++, that it should be possible to define classes that are as easy to use as built in types. Of course there are operators you can define on classes which are not mathematical concepts either. You may wish to overload the == and != operators instead of writing an isEqual method, and might want to overload = because the compiler's default assignment operator is not what you want.
On the other hand, overloading an operator to do something unexpected, like defining ^ to translate a string to Japanese, is obscure and dangerous. Your fellow programmers will not be happy to find out that what looked like an exclusive or comparison was actually something very different. The solution then is to write your classes to make it easy to write clear and maintainable code, whether that means using operator overloading or avoiding it.
Adding two vectors is too ambiguous to warrent defining an operator. As others have shown, many people have different ideas of what this means, whereas for a string it is universally understood that adding two strings together means concatenation. In your example, it isn't entirely clear whether you want to do a component wise add on all the elements, add an element to the end, or concat two vectors together. Although it may be more concise to do it that way, using operator overloading to create your own programming language isn't the best way to go.
*Yes, I know Stroustrup overloaded << and >> to do stream operations rather than bitshifts. But those weren't used often compared to arithmetic and pointer operators in the first place, and it could be argued that now that everyone knows how to use cout, it's generally understood that << and >> are the inserter and extractor operators. (He originally tried to use just < and > for input and output, but the meaning of those operators was so ingrained in everyone's minds that the code was unreadable.)
insert
does already. Also there are two plausible meanings for addition of vectors - concatenation as you've done here, or pairwise addition likevalarray
does, requiring operands of equal size. So it's not an ideal candidate for operator overloading. – Suv[i] += v[i]
over all the elements. – Playsuitoperator+=()
as pairwise addition is because the purpose of a vector is to contain elements. Operating on the elements is what I would consider beyond the design/responsibility of vector. Based on this then by principle of least privilegeoperator+=()
should be assumed to be vector insertion. – Choralevector
is a fundamentally different data type in C++ than Octave or Matlab -- the former implements the computer science definition of 'vector' (a one-dimensional array) while the latter implement the algebraic definition of 'vector' (a one-dimensional matrix). Because they're fundamentally different, they should be expected to behave differently and to be used differently. – Reserpinechar
. In many respects, computer science is algebra. – Langoustevector<complex<double>>
? – Langoustestd::vector
. This is really a pointless debate. – Reserpinevector<double> v1, v2;
and laterv1 += v2;
it could get ambiguous. – Choralev1 + v2
to perform pairwise addition and return a new vector, right?+=
is supposed to be related to+
, but do the addition in place. – Langouste