Rules for Overloading Operators
Every programmer knows the concept of operator overload in C++. Although it seems easy to redefine overload operators, there are some restrictions and limitations to overload operators.
Among them are listed below:
1. Only existing carriers may be overloaded. New operators may not be overloaded.
2. The overloaded operator must have one or more operands of the user-defined type.
3. There is no way to change the fundamental meaning of an operator. In other words, we cannot define the plus (+) operator to subtract a value from the other.
4. Overwritten operators follow the syntax rules for the original operators. These can’t be overridden.
5. Some operators can not be overloaded like operator size (sizeof), membership operator(.),a pointer to a member operator(.*), scope resolution operator(::), conditional operators(?:) etc.
6. We cannot use ―friend‖ functions to overload certain operators. However, member functions can be used to overload them. Friend Functions cannot be used with assignment operator(=), function call operator(()), subscribing operator([]), class member access operator(->) etc.
7. Unary operators, overloaded using a member function, do not take explicit arguments or return explicit values, but, those overloaded by means of a friend function, take a reference argument (the object of the relevant class).
8. Binary operators overloaded through a member function take an explicit argument and the ones that are overloaded by a friend function take two explicit arguments.
9. When using binary operators overloaded with a member function, the operand on the left should be an object of the class concerned.
10. Binary arithmetic operators like +,-,*, and/ have to explicitly return a value. They should not try and alter their own arguments.