Can somebody explain to me what overloading an operator is, and what purpose does it serve? I've red about overloading operators for objects (e.g +, -, >>, << etc.) but i'm failing to understand the general idea behind operator overloading. Please give examples of the ++ and the >> operator in particular. Thanking you in advance.
Overloading an operator means changing the way the compiler uses an operator based on its arguments. For example, say you had a vector class that stores 2 ints: X and Y. You'd overload most, if not all, of the mathematical operators to support your class. For instance:
The overloaded operator above gives the compiler some idea how it should handle your vector. If you didn't overload the operator, the compiler won't be able to handle it; thus, you receive an error.
In a nutshell, then, an overloaded operator changes the way the compiler handles a user-defined type with a given operator.
Edit:
There's two ways you can call the operator: Implicitly or explicitly.
Implicit: Vector( 1, 2 ) + Vector( 3, 4 ).
Explicit: Vector( 1, 2 ).operator + ( Vector( 3, 4 ) ).