Overloading operators
C++ incorporates the option to use language standard operators between classes in addition to
between fundamental types. For example:
int a, b, c;
a = b + c;
is perfectly valid, since the different variables of the addition are all fundamental types.
Nevertheless, is not so obvious that we can perform the following operation (in fact it is incorrect):
struct { char product [50]; float price; } a, b, c;
a = b + c;
The assignation of a class (or struct) to another one of the same type is allowed (default copy
constructor). What would produce an error would be the addition operation, that in principle is not
valid between non-fundamental types.
But thanks to the C++ ability to overload operators, we can get to do that. Objects derived from
composed types such as the previous one can accept operators which would not be accepted
otherwise, and we can even modify the effect of operators that they already admit. Here is a list of
all the operators that can be overloaded:
+ - * / = < > += -= *= /= << >>
<<= >>= == != <= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= [] () new delete
To overload an operator we only need to write a class member function whose name is operator
followed by the operator sign that we want to overload, following this prototype:
type operator sign (parameters);
Here you have an example that includes the operator +. We are going to sum the bidimensional
vectors a(3,1) and b(1,2). The addition of two bidimensional vectors is an operation as simple
as adding the two x coordinates to obtain the resulting x coordinate and adding the two y
coordinates to obtain the resulting y. In this case the result will be (3+1,1+2) = (4,3).

// vectors: overloading operators example
#include <iostream.h>
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp); }
int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0; }

Output

4,3


If you are baffled seeing CVector so many times, consider that some of them make reference to
the class name CVector and others are functions with that name. Do not confuse them:
CVector (int, int); // function name CVector (constructor)
CVector operator+ (CVector); // function operator+ that returns CVector type
The function operator+ of class CVector is the one that is in charge of overloading the arithmetic
operator +. This one can be called by any of these two ways:
c = a + b;
c = a.operator+ (b);
As well as a class includes by deafult an empty and a copy constructor, it also includes a default
definition for the assignation operator (=) between two classes of the same type. This copies the
whole content of the non-static data members of the parameter object (the one at the right side of
the sign) to the one at the left side. Of course, you can redefine it to any other functionality that
you want for this operator, like for example, copy only certain class members.
The overload of operators does not force its operation to bear a relation to the mathematical or
usual meaning of the operator, although it is recommended. For example, it is not very logical to
use operator + to subtract two classes or operator == to fill with zeros a class, although it is
perfectly possible to do so.
Although the prototype of a function operator+ can seem obvious since it takes the right side of
the operator as the parameter for the function operator+ of the left side object, other operators are
not so clear. Here you have a table with a summary on how the different operator functions must
be declared (replace @ by the operator in each case):

This article has been dead for over six months. Start a new discussion instead.