From a second-level question, we can see the member function overload operators and the friends function overload operators.

Source: Internet
Author: User

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 ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.