C++ Overloading operators and overloading functions
C++ allows a certain [function] in the same scopeFunctionandOperatorsSpecify multiple definitions, respectively calledFunction overloadingandOperator overloading。
An overload declaration is a declaration that has the same name as a function or method previously declared in that scope, but their parameter lists and definitions (implementations) are different.
When you call aOverloaded functionorOverloaded operatorthe compiler decides which definition is most suitable by comparing the parameter types you use with the parameter types in the definition. The process of selecting the most suitable overloaded function or overloaded operator is calledOverload resolution。
Function overloading in C++
In the same scope, you can declare several functions with similar functionality and the same name, but these same-named functions must have different formal parameters (referring to the number, type, or order of parameters). You cannot overload functions based only on different return types.
In the following example, functions with the same nameprint()It is used to output different data types:
Example
When the above code is compiled and executed, it produces the following results:
整数为: 5 浮点数为: 500.263 字符串为: Hello C++
Operator overloading in C++
You can redefine or overload most of C++'s built-in operators. In this way, you can use operators with custom types.
Overloaded operators are functions with special names. The function name is composed of the keyword `operator` followed by the operator symbol to be overloaded. Like other functions, an overloaded operator has a return type and a parameter list.
Box operator+(const Box&);
Declare the addition operator to add two Box objects and return the final Box object. Most overloaded operators can be defined as ordinary non-member functions or as class member functions. If we define the above function as a non-member function of the class, then we need to pass two parameters for each operation, as shown below:
Box operator+(const Box&, const Box&);
The following example demonstrates the concept of operator overloading using member functions. Here, objects are passed as arguments, and the object's properties are accessed usingthisthe operator, as shown below:
Example
When the above code is compiled and executed, it produces the following results:
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
Overloadable operators / Non-overloadable operators
The following is a list of operators that can be overloaded:
| Binary arithmetic operators | + (addition), - (subtraction), * (multiplication), / (division), % (modulo) |
| Relational Operators | == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to) |
| Logical Operators | || (logical OR), && (logical AND), ! (logical NOT) |
| Unary operator | + (positive), - (negative), * (dereference), & (address-of) |
| Increment and decrement operators | ++ (increment), -- (decrement) |
| Bitwise Operators | | (bitwise OR), & (bitwise AND), ~ (bitwise NOT), ^ (bitwise XOR), << (left shift), >> (right shift) |
| Assignment Operators | =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>= |
| Memory allocation and release | new, delete, new[ ] , delete[] |
| Other Operators | ()(function call),->(member access),,(comma),[](subscript) |
The following is a list of operators that cannot be overloaded:
- .Member access operator
- .*, ->*Member pointer access operator
- ::: scope operator
- sizeof: length operator
- ?:: conditional operator
- #: preprocessing symbol
Operator overloading example
The following provides various examples of operator overloading to help you better understand the concept of overloading.