**1. What is Operator overload?**

Operator overloading can be divided into two parts: "operator" and "overload ". It should be no stranger to heavy load. This isPolymorphism during compilation. overloading can be divided into function overloading and operator overloading. The difference between operator overloading and function Overloading is that operator overloading must be an operator. Let's take a look at the so-called Operator Overloading:

1 # Include <iostream> 2 3 Using Namespace STD; 4 5 Int Main () 6 { 7 Int A = 2 , B = 3 ; 8 Float C = 2.1f , D = 1.2f ; 9 Cout < " A + B = " <A + B < Endl; 10 Cout < " C + D = " <C + D < Endl; 11 Return 0 ; 12 }

We can see that the operator "+" is complete.**Float**And**Int**Two types of addition calculation, which is the heavy load of operators. These built-in types of Operator Overloading have been implemented, but what if we want to implement a similar addition operation for the classes we have already written ?? For example, now there is such a point class. To add two vertices, the result is that the horizontal and vertical coordinates must be added. At this time, we need to write an operator to reload the function.

View code

1 # Include <iostream> 2 3 Using Namespace STD; 4 5 Class Point 6 { 7 Double X; 8 Double Y; 9 Public : 10 Double Get_x () 11 { 12 Return X; 13 } 14 Double Get_y () 15 { 16 Return Y; 17 } 18 Point ( Double X = 0.0 , Double Y = 0.0 ): X (x), y (y ){}; 19 Point Operator + (Point P ); 20 }; 21 // Overload operator "+" 22 Point point :: Operator + (Point P) 23 { 24 Double X = This -> X + P. X; 25 Double Y =This -> Y + P. Y; 26 Point tmp_p (x, y ); 27 Return Tmp_p; 28 } 29 Int Main () 30 { 31 Point P1 ( 1.2 ,3.1 ); 32 Point P2 ( 1.1 , 3.2 ); 33 Point P3 = p1 + P2; 34 Cout <p3.get _ x () < " " <P3.get _ y () < Endl; 35 Return 0 ; 36 }

**2. Two Methods for implementing Operator Overloading**

There are two methods to implement the operator overload, namely through the "youyuan function" or "class member function ".

1. Format of uyuanyuan function overload operators:

1 ClassClass Name2 {3Friend return typeOperatorOperator (parameter table );4 };5 //Out-of-class definition format:6 Return Operator (parameter table)7 {8//Function body9}

2. Format of implementing Operator Overloading for class member functions:

1 Class Class Name 2 { 3 Public : 4 Return type Operator Operator (parameter table ); 5 }; 6 // Out-of-class Definition Format 7 Return type Class Name ::Operator Operator (parameter table) 8 { 9 // Function body 10 }

In this case, it is not enough to compare the differences between the two implementation methods. We use two implementation methods to write the "+" and "-" overloading of the Point class respectively.CodeAs follows:

View code

1 # Include <iostream> 2 3 Using STD: Endl; 4 Using STD: cout; 5 6 Class Point 7 { 8 Double X; 9 Double Y; 10 Public : 11 Double Get_x () 12 { 13 Return X; 14 } 15 Double Get_y () 16 { 17 Return Y; 18 } 19 Point ( Double X = 0.0 , Double Y = 0.0 ): X (x), y (y ){}; 20 Friend point Operator - (Point P1, point P2 ); 21 PointOperator + (Point P ); 22 }; 23 // Overload operator "-" 24 Point Operator - (Point P1, point P2) 25 { 26 Double X = p1.get _ x ()- P2.get _ x (); 27 Double Y = p1.get _ y ()- P2.get _ y (); 28 Point P3 (x, y ); 29 Return P3; 30 } 31 // Overload operator "+" 32 Point point :: Operator + (Point P) 33 { 34 Double X = This -> X + P. X; 35 Double Y = This -> Y + P. Y; 36 Point tmp_p (x, y ); 37 Return Tmp_p; 38 } 39 Int Main () 40 { 41 Point P1 ( 1.2 , 3.2 ); 42 Point P2 ( 1.1 , 3.1 ); 43 Point P3 = p1 + P2; 44 Point P4 = Operator - (P1, P2 ); 45 Cout <p3.get _ x () < " " <P3.get _ y () < Endl; 46 Cout <p4.get _ x () < " " <P4.get _ y () < Endl; 47 Return 0 ; 48 }

I don't know if we can see it here. When we use the friend functions to overload the binary operator "-", the form parameters are two. When using the class member functions, the form parameters have only one. In this case, because this pointer exists in the class member function, this is equivalent to a parameter, so the class member implementation operator overload requires one fewer formal parameter than the original one, such: the class member function is used to implement the unary operator "-", so no parameter is required. For this reason, there are restrictions on the operator overloading implemented by youyuan functions, such as [], (),-> and =. You cannot use youyuan functions to implement the operator overloading.

In actual development, we recommend that you reload the single-object operator as a member function, while the binary operator is recommended to be a friend function. Generally, the binary operator overload is more convenient than the member function overload, but sometimes the binary operator must be overloaded as a member function, such as the value assignment operator =. If you want to modify the internal status of an object, you can use the class member function to modify it.

** **

**Iii. Principles of Operator Overloading**

In this way, Operator Overloading is quite simple. In fact, Operator Overloading also follows some principles:

1. Only the existing C ++ operators can be overloaded in C ++, and new operators cannot be defined by users.

2. The vast majority of operators in C ++ can be overloaded, except for member access Operators**.**, Scope Operator**::,**Length Operator**Sizeof**And conditional Operators**? :**.

3. After the operator is overloaded, the number of operation objects (operands) of the operator cannot be changed. For example, "+" is an operator that implements two operands. It is still a binary operator after being overloaded.

4. Overloading does not change the original priority of the operator and the original combination.

6. Operator Overloading cannot all be pre-defined basic data in C ++. The purpose of this operation is to prevent users from modifying the operator properties used for basic data types.

** **

**4. Why Operator Overloading?**

So many principles need to be followed for operator overloading, why is Operator overloading required? Why didn't I write an add () function instead of operator + ?? In my opinion, the reason why C ++ must support Operator Overloading is to unify operations with built-in data types, such as C = a + B and c = add (A, B ), which one looks more intuitive? It is obviously the former. At the same time, we hope that the data types defined by ourselves can be as convenient as the built-in data types int and double. It may be a bit difficult to give an example of this addition. Now the operators that add heavy loads are [], <, ^, |, and so on? In this case, what member functions should we use instead ?? What is the effect after replacement? What will this function do at a glance ??

** **

**Thinking: There are two types of ++ operators in C ++. How can we distinguish between pre-auto-increment and post-auto-increment?**

** **

**Let's take a look at some of the lessons learned.**