C++ rules for overloading operators
Although it looks simple to redefine the operators, There are certain restrictions and limitations in overloading them, some of them are listed below
1. Only existing operators can be overloaded. now operators cannot be created
2. The overloaded operator must have at least one operand that is type of user defined.
3. We can’t change the basic meaning of an operator. That is to say we cannot redefine the plus(+) operator to subtract one value from the other.
4.The Overloaded operators follows the rules of the original operators. They cannot be overridden.
5 There are some operators that can’t be overloaded.
6 we can’t use friend functions to overload certain operators. however the member function can be used to overload them .
7.The unary operators, overloaded by the member function, take no explicit arguments and also no explicit values return, but those overloaded by means of a friend function, take one reference argument.
8. Binary operator overloaded through a member function take two explicit arguments
9. When using the binary operators overloaded using a member function, the left hand side operand must be an object of the relevant class.
10. Binary arithmetic operators such as *,+,- and / must be explicitly return the value, they must not attempt to change their own arguments.
OPERATORS that can not be overloaded |
Sizeof |
size of operator |
.
* |
Membership operator |
:: |
scope resolution operator |
?:
|
Conditional operator |
Where a friend cannot be used |
= |
assignment operator |
() |
Function call operator |
[] |
subscripting operator |
-> |
class member access operator |