Rules for Overloading Operators
- Only existing operators can be overloaded. New operators cannot be created.
- The overloaded operator must have atleast one operand that is of user defined type.
- The basic meaning of an operator cannot be changed. That is the plus operator cannot be used to subtract one value from the other.
- Overloaded operator follow the syntax rules of the original operators. They cannot be overridden.
- There are some operators that cannot be overloaded. They are Size of, . ,: :,?:.
- Friend function cannot be used to overload certain operators ( = ,( ) ,[ ] ,->).However member functions can be used to overload them.
- Unary operators, overload by means of a member function, take no explicit arguments and return no explicit values,but,those overloaded by means of a friend function, take one reference argument.
- Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
- When using binary operator overloaded through a member function, the left hand operand must be an object of the relevant class.
- Binary arithmetic operators such as +,-,*,and / must explicitly return a value. They must not attempt to change their own arguments.
|