C++ operator overload
1. What is operator overloading?
Operator overloading is a feature of C++ that allows programmers to redefine the behavior of operators for custom types such as classes and structures. Through operator overloading, we can use familiar operator syntax to manipulate objects of custom types, making the code more concise and intuitive.
When operators are used for objects of class type, C++ language allows us to specify new meanings through operator overloading. C++ stipulates that when a class type object uses operators, it must be converted to call the corresponding operator overload. If there is no corresponding operator overload, an error will be compiled.
For example, we can overload a custom plural class+
Operators, allowing two plural objects to be used directly+
To add:
Complex a(1, 2); Complex b(3, 4); Complex c = a + b; // Use overloaded + Operators
2. Syntax rules for operator overloading
In C++, operator overloading is implemented by defining special member functions or non-member functions.
The basic syntax is as follows:
Return type operatorOperators(Parameter list) { // Function body}
in:
-
operator
is a C++ keyword that declares an operator overload function. -
Operatorsis the operator to be overloaded, such as
+
,-
,*
,/
wait. - Return typeIt is the return value type of the operator overload function, which is usually related to the type of the operand.
- Parameter listIt is the parameter of the operator overloading function. The number and type of parameters depend on the type of operator and the overloading method.
Notice:
- Which operators need to be overloaded in a class? It depends on which operators are meaningful after overloading.
- When overloading the ++ operator, there are pre-++ and post-++. The operator overloads the function names are both operator++, which cannot be distinguished well. C++ stipulates that when post-++ overloading, an int formal parameter is added to form a function overload with pre-++, which is convenient for distinguishing.
- When operators are used for objects of class type, C++ language allows us to specify new meanings through operator overloading. C++ stipulates that when a class type object uses operators, it must be converted to call the corresponding operator overload. If there is no corresponding operator overload, an error will be compiled.
- Operator overloading is a function with a special name, and its name is composed of the operator and the operator to be defined later. Like other functions, it also has its return type and parameter list and function body.
- The number of parameters of the overloaded operator function is as many as the number of operation objects that the operator functions. The first-party operator has one parameter, and the ⼆ meta operator has two parameters. The left-side operation object of the meta operator is passed to the first parameter, and the right-side operation object is passed to the second parameter.
- If an overloaded operator function is a member function, its first operation object is passed to the implicit this pointer by default. Therefore, when an operator overloads as a member function, there are one less parameter than the operation object.
- After operator overloading, its priority and binding are consistent with the corresponding built-in type operator.
- New operators cannot be created by connecting symbols that are not in the syntax: such as operator@.
When overloading << and >>, it is necessary to overload as a global function. Because overloading is a member function, this pointer preempts the first formal parameter position by default. The first formal parameter position is the left-hand operation object, and it becomes an object <<cout when called, which does not conform to usage habits and readability.
Overload the global function and put the ostream/istream at the first formal parameter position, and the first formal parameter position is the class type object.
Member function overload
Operator overloading functions can be defined as member functions of a class. In this case, the number of parameters of the function is one less than the operand of the operator, because the first operand is passedthis
Pointer is implicitly passed.
Non-member function overload
Operator overloading functions can also be defined as non-member functions (global functions or friend functions).
In this case, the number of parameters of the function is the same as the operand of the operator.
Assign operator overloading
Assignment operator overloading is a default member function that is used to complete the direct copy assignment of two existing objects. This should be different from the copy construct. The copy construct is used to initialize one object to another object to be created.
Features of assignment operator overloading:
- Assignment operator overloading is an operator overloading, which stipulates that it must be overloaded as a member function. It is recommended to write the parameters of the overloaded assignment operation as a reference to the current class type, otherwise the value will be passed and the parameters will be copied.
- There is a return value, and it is recommended to write it as a reference of the current class type. Reference return can improve efficiency. The purpose of having a return value is to support continuous assignment scenarios.
- When there is no explicit implementation, the compiler will automatically generate a default assignment operator overload. The default assignment operator overload behavior is similar to the default copy constructor. The built-in type member variable will complete the value copy/shallow copy (one byte and one byte copy) and the custom type member variable will call its assignment overload function.
Copy construction: One existing object initializes another object to be instantiated; Assignment overload: The copy between two existing objects, note that the return value is the class type, and deals with continuous assignments.
Address operator overload
const member function
- The member function modified by const is called a const member function, and the member function modified by const is placed behind the member function parameter list.
- const actually modify this pointer implicitly in this member function, indicating that any member of the class cannot be modified in this member function. Date* const this becomes const Date* const this
Address operator overload
- Address operator overloading is divided into ordinary address operator overloading and const address operator overloading. Generally, these two function compilers are automatically generated by us, and there is no need to display the implementation.
- Unless in some very special scenarios, such as if we don’t want others to get the address of the current class object, we can implement a copy by ourselves and return an address randomly.
3. Overloadable operators and non-overloadable operators
Overloadable operators
Most operators in C++ can be overloaded, including:
- Arithmetic operators:
+
,-
,*
,/
,%
,++
,--
- Comparison operator:
==
,!=
,<
,>
,<=
,>=
- Logical operators:
&&
,||
,!
- Bit operator:
&
,|
,^
,~
,<<
,>>
- Assignment operator:
=
,+=
,-=
,*=
,/=
,%=
,&=
,|=
,^=
,<<=
,>>=
- Other operators:
[]
,()
,->
,,
,*
(Decision),&
(Address)
Not overloadable operators
A few operators in C++ cannot be overloaded, including:
- Scope parsing operator:
::
- Member access operator:
.
and.*
- Conditional operator:
?:
- Type conversion operator:
typeid
anddynamic_cast
wait - Compilation time operator:
sizeof
5. Notes on operator overloading
When using operator overloading, you need to pay attention to the following points:
-
Follow the original semantics of operators: Operator overloading should maintain the original semantics of the operator to avoid confusion. For example, overloading
+
Operators should implement the semantics of addition, not subtraction. - Don't abuse operator overloading: While operator overloading can make the code more concise, overuse or abuse can make the code difficult to understand and maintain.
- Keep operators precedence and bonding: Operator overloading cannot change the priority and binding of operators, which is stipulated by the language.
-
Correctly handle const objects: If the operator overload function does not modify the state of the object, it should be declared as
const
Member function. - Pay attention to memory management: When overloading assignment operators and copy constructors, special attention should be paid to the problems of deep copy and shallow copy to avoid memory leakage.
- Avoid creating new operators: C++ does not allow creating new operators, and can only overload existing operators.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.