From a second-level question, we can see the member function overload operators and the friends function overload operators.
First question: Which of the following operators can be reloaded by the friend functions:
A) =, + ,-,\
B) [], +, (), new
C)->, +, *,>
D) <, >>, + ,*
The correct answer is D.
We know that in the operator overload, the difference between the operator overload function of the youyuan function and the member operator overload function is: The youyuan function does not have the this pointer, and the member function has, therefore, in the overload of the two operands, the Friends function has two parameters, and the member function has only one.
Therefore, we can summarize the following:
1. For binary operators, the function parameter table of the member function overload operator has only one parameter, while the UFIDA function overload operator function parameter table contains two parameters.
For single-wood operators, the function parameter table of the member function overload operator does not have a parameter, while the UFIDA function overload operator function parameter table contains a function. This problem should be clarified. There is a problem with this pointer...
2. Binary operators can be reloaded by using friend functions and member functions. However, in one case, you can only use friend functions.
That is, the variable on the left of the binary operator is a constant, not an object !!! This is very important.
You can remember the following experiences about Operator Overloading:
1. We recommend that you select a member function for the single-object operator;
2. The operator "=, (), [],->" can only be used as a member function;
3. for operators "+ =,-=,/=, * =, & = ,! = ,~ =, % =, <<=, >>= "We recommend that you reload the function as a member function;
4. For other operators, we recommend that you reload them as friend functions.
Then the answer to this question is obvious:
Reload the "+" In x + y * z with the member function. The "*" UFIDA function overload should be written :?
Answer: x. operator + (operator * (y, z ))