Rules for overloading operators

  • Only existing operators can be overloaded. New operators cannot be created.
  • The overloaded operator must have at least one operand that is of the user-defined type.
  • We cannot change the meaning of an operator. That is, we cannot redefine the plus (+) operator to subtract one value from others.
  • Overloaded operators follow the syntax rules of the original operators. That cannot be overridden.
  • Friend functions cannot be used to overload certain operators like =, ( ), [ ] and >.
  • Binary operators such as +, -, *, and / must explicitly return a value
  • As described above, all operators cannot be overloaded.
  • Unary operators overloaded by means of a member function take no explicit arguments and return no explicit values. But, those overloaded by means of friend function take one argument.
  • Binary operators overloaded by means of a member function take one explicit argument. But, those overloaded by means of friend function take two arguments.
  • The process of operator overloading generally involves following steps.
  • Declare a class whose objects are to be manipulated using operators.
  • Declare the operator function, in the public part of the class. It can either normal member function or friend function.
  • Define the operator function within the body of a class or outside the body of the class but function prototype must be inside the class body.



Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours