This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
General Rules for Operator Overloading
- 7 contributors
The following rules constrain how overloaded operators are implemented. However, they do not apply to the new and delete operators, which are covered separately.
You cannot define new operators, such as . .
You cannot redefine the meaning of operators when applied to built-in data types.
Overloaded operators must either be a nonstatic class member function or a global function. A global function that needs access to private or protected class members must be declared as a friend of that class. A global function must take at least one argument that is of class or enumerated type or that is a reference to a class or enumerated type. For example:
The preceding code sample declares the less-than operator as a member function; however, the addition operators are declared as global functions that have friend access. Note that more than one implementation can be provided for a given operator. In the case of the preceding addition operator, the two implementations are provided to facilitate commutativity. It is just as likely that operators that add a Point to a Point , int to a Point , and so on, might be implemented.
Operators obey the precedence, grouping, and number of operands dictated by their typical use with built-in types. Therefore, there is no way to express the concept "add 2 and 3 to an object of type Point ," expecting 2 to be added to the x coordinate and 3 to be added to the y coordinate.
Unary operators declared as member functions take no arguments; if declared as global functions, they take one argument.
Binary operators declared as member functions take one argument; if declared as global functions, they take two arguments.
If an operator can be used as either a unary or a binary operator ( & , * , + , and - ), you can overload each use separately.
Overloaded operators cannot have default arguments.
All overloaded operators except assignment ( operator= ) are inherited by derived classes.
The first argument for member-function overloaded operators is always of the class type of the object for which the operator is invoked (the class in which the operator is declared, or a class derived from that class). No conversions are supplied for the first argument.
Note that the meaning of any of the operators can be changed completely. That includes the meaning of the address-of ( & ), assignment ( = ), and function-call operators. Also, identities that can be relied upon for built-in types can be changed using operator overloading. For example, the following four statements are usually equivalent when completely evaluated:
This identity cannot be relied upon for class types that overload operators. Moreover, some of the requirements implicit in the use of these operators for basic types are relaxed for overloaded operators. For example, the addition/assignment operator, += , requires the left operand to be an l-value when applied to basic types; there is no such requirement when the operator is overloaded.
For consistency, it is often best to follow the model of the built-in types when defining overloaded operators. If the semantics of an overloaded operator differ significantly from its meaning in other contexts, it can be more confusing than useful.
Operator Overloading
Was this page helpful?
Additional resources
21.12 — Overloading the assignment operator
- C++ Data Types
- C++ Input/Output
- C++ Pointers
- C++ Interview Questions
- C++ Programs
- C++ Cheatsheet
- C++ Projects
- C++ Exception Handling
- C++ Memory Management
C++ Assignment Operator Overloading
Prerequisite: Operator Overloading
The assignment operator,”=”, is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types.
- Assignment operator overloading is binary operator overloading.
- Overloading assignment operator in C++ copies all values of one object to another object.
- Only a non-static member function should be used to overload the assignment operator.
In C++, the compiler automatically provides a default assignment operator for classes. This operator performs a shallow copy of each member of the class from one object to another. This means that if we don’t explicitly overload the assignment operator, the compiler will still allow us to assign one object to another using the assignment operator ( = ), and it won’t generate an error.
So, when we should perform assignment operator overloading? when our class involves dynamic memory allocation (e.g., pointers) and we need to perform a deep copy to prevent issues like double deletion or data corruption.
here, a and b are of type integer, which is a built-in data type. Assignment Operator can be used directly on built-in data types.
c1 and c2 are variables of type “class C”.
The above example can be done by implementing methods or functions inside the class, but we choose operator overloading instead. The reason for this is, operator overloading gives the functionality to use the operator directly which makes code easy to understand, and even code size decreases because of it. Also, operator overloading does not affect the normal working of the operator but provides extra functionality to it.
Now, if the user wants to use the assignment operator “=” to assign the value of the class variable to another class variable then the user has to redefine the meaning of the assignment operator “=”. Redefining the meaning of operators really does not change their original meaning, instead, they have been given additional meaning along with their existing ones.
Also, always check if the object is not being assigned to itself (e.g., if (this != &other)), as assigning an object to itself does not make sense and may cause runtime issues.
While managing dynamic resources, the above approach of assignment overloading have few flaws and there is more efficient approach that is recommended. See this article for more info – Copy-and-Swap Idiom in C++
Similar Reads
- cpp-operator
- cpp-operator-overloading
IMAGES
VIDEO