Rules of Operator Overloading in C++

Rules of Operator Overloading in C++

Operator overloading is a type of static or compile-time polymorphism. C++ supports the compile-time polymorphism. The function overloading and the operator overloading are common examples of compile-time polymorphism.

Operator Overloading in C++

Let’s see the rules for the operator overloading.

  1. Only built-in operators like (+, -, *, /, etc)can be overloaded.
  2. We cannot overload all of those operators that are not a part of C++ language like ‘$’.
  3. We can’t change the arity of the operators. The arity of an operator is the number of operands that the operator takes.
  4. We can overload the unary operator as an only unary operator, and we cannot overload it as a binary operator and similarly, We can overload binary operators as an only binary operator, and we cannot overload it as a unary operator.
  5. During the operator overloading, we cannot change the actual meaning of an operator. For example, We cannot overload the plus(+) operator to subtract one value form the other value.
  6. The precedence of the operators remains the same during operator overloading.
  7. The operator overloading is not possible for built-in data types. At least one user-defined data types must be there.
  8. Some operators like assignment “=”, address “&” and comma “,” are by default overloaded.
  9. When using binary operators overloaded through a member function, the left-hand operand must be an object of the relevant class.

List of operators that can be overloaded

List of operators that can be overloaded are mentioned below;

+ * / % ^
|= *= <<= >>= [] ()
delete delete [] new new [] -> ->*
<< >> == != && ||
+= -= /= %= ^= &=
& | ~ ! , =
< > <= >= ++

More Operator Overloading Programs

List of operators that cannot be overloaded

List of operators that cannot be overloaded are mentioned below;

  • Scope Resolution Operator (::)
  • Pointer-to-member Operator (.*)
  • Member Access or Dot operator (.)
  • Ternary or Conditional Operator (?:)
  • Object size Operator (sizeof)
  • Object type Operator (typeid)

How Operator Overloading works with functions in C++

Operator overloading can be done by implementing a function and the function can be a;

  1. Member Function
  2. Non-Member Function
  3. Friend Function

The Member Function and Non-Member Function: Operator overloaded function can be a member function of class X if the Left operand is an object of that class X, but if the Left operand is different, then Operator overloading function must be a non-member function.

The Friend Function: Operator overloaded function can be made friend function of class X if it needs access to the private and protected members of class X.

Operator overloading Examples in C++ [with error]

Output

[Error] no match for ‘operator++’ (operand type is ‘T4Tutorials’)

Operator overloading Examples in C++ [correct = without error]

This program is showing the operator overloading of the prefix increment operator.

Output

The operator is overloaded successfully: 1

Operator overloading of Postfix increment operator in C++

This program is showing the operator overloading of the postfix increment operator.

Output

The operator is overloaded successfully: 1

Operator overloading of Postfix decrement operator in C++

This program is showing the operator overloading of the postfix decrement operator.

Output

The operator is overloaded successfully: -1

Video Lecture

Add a Comment