Rules for operator overloading in C++

C++Server Side ProgrammingProgramming

In C++ it supports compile time polymorphism. The examples of compile time polymorphism are the function overloading and the operator overloading.

There are some rules for the operator overloading. These rules are like below

  • Only built-in operators can be overloaded. If some operators are not present in C++, we cannot overload them.

  • The arity of the operators cannot be changed

  • The precedence of the operators remains same.

  • The overloaded operator cannot hold the default parameters except function call operator “()”.

  • We cannot overload operators for built-in data types. At least one user defined data types must be there.

  • The assignment “=”, subscript “[]”, function call “()” and arrow operator “->” these operators must be defined as member functions, not the friend functions.

  • Some operators like assignment “=”, address “&” and comma “,” are by default overloaded.

Updated on 30-Jul-2019 22:30:25