C + + operator overloading
The operands of predefined operators in C + + can only be basic data types.
But in fact, similar operations are required for many user-defined types (such as classes).
Therefore, you can redefine or overload most of the built-in operators in C + +. This allows you to use custom type operators.
Operator overloading is realized by creating an operator function, which defines the operation to be performed by the overloaded operator. The definition of operator function is similar to that of other functions. The only difference is that the function name of operator function is composed of the keyword operator and the operator symbols to be overloaded.
Overloaded operators are functions with special names. The function name is composed of the keyword operator and the operator symbols to be overloaded. Like other functions, overloaded operators have a return type and a parameter list.
1. + operator overload
Overload the + operator, we can redefine its meaning for +.
For example:
// Use the built-in + operator for int type operations int a = 10; int b = 20; int c = a + b; // The result is 30
If we use the + operator to operate our custom data type, we need to overload the + operator.
For example, there is a custom data type, Person, which has two attributes: height and weight
class Person { public: int height; // height int weight; // weight Person(int height, int weight) { this->height = height; this->weight = weight; } };
We define two objects p1 and p2;
Person p1(10,10); // Set the two properties of p1 to 10, 10 Person p2(20,20); // Set the two properties of p2 to 20, 20
If we want to add height and weight to p1 and p2 respectively, we need:
int heigt = p1.height + p2.height; int weight = p1.weight + p2.weight;
Of course, we can also overload the + operator:
There are two ways:
- Member overloading
// Member function overload+ Person operator+(Person &p) { Person temp; // Create a Person object temp to temporarily store the added value of the two objects temp.height = this->height + p.height; temp.weight = this->weight + p.weight; return temp; // Return temp, which is an object of type Person }
The nature of member function overloading:
cout << p1.operator+(p2).height << endl;
It can be abbreviated as: cout < < (P1 + P2) height << endl;
- Global function overload 1
// Global function overload+ Person operator+(Person& q, Person& p) { Person temp; temp.height = q.height + p.height; temp.weight = q.weight + p.weight; return temp; }
This operation can only be performed in two. After VS2019, the result of the overloaded + operator is a pure right value and cannot be calculated as an left value. Therefore, the continuous addition of multiple class instances will report an error. Can only be added in pairs.
- Global function overload 2
//A section of memory is opened in the global area and not in the global function to prevent the system from recycling after the function is executed Person temp; // Global function overload+ Person& operator+(Person& q, Person& p) { temp.height = q.height + p.height; temp.weight = q.weight + p.weight; return temp; }
Return the reference and be careful not to open up space in the stack.
This can realize chain operation.
The essence of global function overloading:
cout << operator+(p1, p2).height << endl;
It can be abbreviated as: cout < < (P1 + P2) height << endl;
// Output verification cout << (p1 + p2).height << endl; cout << (p1 + p2).weight << endl;
If there are more than two objects to operate: chain operation
Person p1(10, 10); // Set the two properties of p1 to 10, 10 Person p2(20, 20); // Set the two properties of p2 to 20, 20 Person p3(30, 30); // Set the two properties of p3 to 30, 30 Person p4(40, 40); // Set the two properties of p3 to 40, 40 Person p5(50, 50); // Set the two properties of p3 to 50, 50 // Output verification cout << (p1 + p2 + p3 + p4 + p5).height << endl; //Output 150 (10 + 20 + 30 + 40 + 50) cout << (p1 + p2 + p3 + p4 + p5).weight << endl; //Output 150 (10 + 20 + 30 + 40 + 50)
It can be seen that the appropriate use of operator overloading can make the program more concise and convenient for programming.
-Operator overloading is similar to + and can be programmed according to your own needs.