What Are the Basic Rules and Idioms for Operator Overloading in C++?

In C++, operator overloading is a form of compile-time polymorphism where an operator is redefined to provide a special meaning beyond its typical operation. It allows developers to use traditional operators with user-defined types (or classes). In this article, we will learn the basic rules and idioms that one should remember for operator overloading in C++.

Rules and Idioms for Operator Overloading in C++

Following are the fundamental rules and idioms that should be followed when overloading operators in C++:

1. Overload only Built-in Operators

In C++, only existing built-in operators can be overloaded. We cannot create a new operator or rename existing ones, hence if any operator is not present in C++ that operator cannot be overloaded using operator overloading.

Example:

// Invalid: Cannot create new operator 
MyClass operator** (const MyClass& obj) 
{     // operation     
return *this;
 }

2. Do not Change Arity of Operators

Do not alter the number of operands an operator can take. For example any binary operator like “==” will always take two operands only we cannot change the arity of operator being overloaded. An attempt to overload it with a different number of operands would be invalid.

Example:

class MyClass 
{
    // Invalid: '==' operator should take two operands
    bool operator==(const MyClass& myObj1, const MyClass& myObj2) 
{
        // comparison logic
        return true;
    }
};

3. Precedence and Associativity of Operators cannot be Changed

Even after the operator is overloaded, its precedence order and associativity remains same and cannot be changed. We cannot change these properties through operator overloading.

4. Some Operators That Cannot Be Overloaded

In C++, we have certain operators that cannot be overloaded. This is done to maintain the original functionality of those operators to ensure the consistency and predictability of the language’s syntax and semantics. Therefore, C++ standard disallow overloading of these specific operators. These operators include:

  • Scope Resolution Operator (::)
  • Member Access Operator (.)
  • Member Access through Pointer to Member Operator (.*)
  • Ternary Conditional Operator (?:)

5. Overloaded Operators Cannot Have Default Parameters 

While overloading operators never pass default parameters except for the function call operator (), overloaded operators cannot hold default parameters.

Example:

// Invalid: Cannot have default parameters
MyClass operator+ (const MyClass& obj = MyClass()) 
{
    // operation
    return *this;
}

6. Overloading of && and || Operators Lose Short-Circuit Evaluation 

When we overload the logical operators && and ||, they lose their short-circuit evaluation property so avoid doing so and handle such overloading properly.

7. Overloading the Assignment Operator 

While overloading the assignment operator always members that it should return a reference to *this and must be a member function.

Example:

// Overloading '=' operator
MyClass& operator= (const MyClass& obj) 
{
    // operation
    return *this;
}

Conclusion

In Conclusion, operator overloading can definitely make our C++ code cleaner and intuitive, but only if used properly. By following the above rules and idioms we can make sure that operator that we have overloaded behaves in a way that’s consistent, understandable, and useful. Always remember, the main goal of operator overloading is to make our code easier to read and write, not to confuse or trick other programmers.


Contact Us