The basic rules and idioms that must be followed for operator overloading are the operator’s precedence and associativity, or the number of operands, which cannot be changed and can only overload existing operators.
In C++, operator overloading helps developers to redefine the existing operators. It is a form of compile-time polymorphism where an operator is redefined to provide a new meaning beyond its original meaning. In this article, you will learn about the basic rules and idioms that must be followed while overloading operators in C++.
Table of Contents:
Basic Rules for Operator Overloading in C++
Below, we have mentioned a few basic rules that must be followed while overloading an operator in C++:
1. You can Only Overload an Existing Operator
Overloading an existing operator means you can only redefine the meaning of an overloaded operator in C++, as you cannot create a new operator according to our needs.
Example:
Output:
In this code, we are overloading an already existing + operator in C++ to add the real part of the two complex classes to get a new complex class, not creating a new one.
2. You cannot Change the Precedence of an Operator
You cannot change the operator’s precedence as it is fixed in C++, and you also cannot do so by overloading operators. For instance, you cannot make the * operator have higher precedence than the + operator by operator overloading.
Example:
Output:
In this code, 4*2 is evaluated first because the (*) operator has higher precedence over the (+) operator, and then the result is added to 3. The result shows that precedence is fixed in C++, and we cannot change it.
3. Cannot change the Associativity of an Operator
You cannot change the operator’s association because the direction in which the operators are evaluated is also fixed in C++ by operator overloading. Generally, you cannot make the = operator left-associative as it is set as right-associative in C++.
Example:
Output:
In this code, b=c is evaluated first, and then a=b is evaluated. This happens because we are overloading the = operator, which has the right-to-left association in C++ and cannot be changed.
4. Cannot change the Number of Operands
We cannot use any number of operands according to our need for an operator. We must follow the original syntax of each and every operator. For example, we can use only two operands with the binary + operator.
Example:
Output:
In this code, we are overloading the + operator to add the two points p1 & p2, which results in a new point. In C++, the operator’s syntax is fixed, and that’s why the + operator can only have two operands that cannot be changed.
5. Cannot change the Meaning of an Operator on Built-in Types
We cannot change the operators’ behavior with the built-in types like int, double, etc.; we can only overload the operator for user-defined data types.
Example:
Output:
In this code, x + y gives the simple sum of the taken integers, which is its original behavior, and if we overload it for customized data types, the behavior cannot be modified.
6. Must have Clear Documents for the Overloaded Operators
Since operator overloading makes codes complex, we must document the overloaded operators to clearly understand what each operator does.
Example:
Output:
In this code, we are adding the x and y components of the two vectors with an overloaded + operator, which makes the code more complex. And find() displays the results of the addition. So, making clear documentation will help to understand what the + operator does.
Idioms for Operator Overloading in C++
Below are the idioms that help us to overload the operators efficiently and easily:
1. For the Binary Operators
Member functions should be only for object-specific operations, and non-member functions are only used for symmetric operations.
2. For the Unary Operators
The unary operators can be overloaded as member functions because they require only one operand.
3. For the Comparison Operators
The comparison operators can be overloaded as a member function or a non-member function but should return a bool value.
4. For the Assignment Operator (=)
The assignment operator is special and should be handled separately, as it has a standard idiom in C++.
5. For the Insertion(<<) and Extraction(>>) Operators
Operator overloading for the stream operators can be done easily and efficiently, as overloading them is very common in C++ according to custom input and output operations.
Overloading New and Delete Operators in C++
The new and delete operators can also be overloaded globally. Overloading these operators provides debugging, logging, or customized memory management. The new operator is used to allocate the memory, and the delete operator is used to deallocate the memory. They help programmers create and delete variables in the program. These operators are also helpful in debugging and removing invalid or unnecessary data from a program.
Example:
Output:
In this code, we are using the overloaded new and delete operators to allocate and free the customized memory in C++. The new operator displays a message using the malloc(), and the delete operator displays a message using free() after freeing the memory.
Overloading in Template Classes in C++
In C++, templates are the base of generic programming. Templates are simple yet powerful for creating generic functions and classes. Both function overloading and operator overloading can be applied to the template classes. There are two types of templates: function templates, used for the template functions, and class templates, used for the template classes.
In template classes, the overloading is done in the same way as in the normal classes but with the advantage of handling multiple data types dynamically. Also, templates provide writing the reusable and flexible codes with different data types. Combining the templates with the overloading provides more powerful classes, which makes writing codes more efficient.
Example:
Output:
In this code, we are dealing with function overloading in a template class. The overloaded add() functions are used to add two or three numbers and also the doubles simultaneously. The intCalc() function is used to add two or three integers and two doubles, which is printed as a result.
Conclusion
In conclusion, operator overloading enhances the efficiency and expressiveness of the codes. Overloading the new and deleted operators can be used for memory management, and overloading the template classes should be done when dealing with advanced generic techniques.
By following these rules and idioms, we can achieve better code readability, efficiency, and cleanness in the codes in C++.