member function overloading operator and friend function overloading operator from a two-level problem

Source: Internet
Author: User

First question: The following operators can be overloaded with friend functions:

A) =,+,-, \

B) [],+, (), New

C)->,+,*,>>

D) <<,>>,+,*

The correct answer is D

We know that in operator overloading, the difference between the friend function operator overload function and the member operator overload function is that the friend function does not have the this pointer, and the member function has, therefore, the friend function has two parameters in the overload of two operands, and the member function has only one.

Therefore, we can summarize as follows:

1. For binocular operators, there is only one parameter in the function parameter table of the member function overloading operator, and the function parameter table of the UF meta-function overload operator contains two parameters.
For the individual tree operator, there are no parameters in the function parameter table of the member function overloading operator, and the function parameter table of the UF meta-function overload operator contains one. This question to be clear, there is a problem with this pointer ...
2. Binocular operators can generally be overloaded with the element function and member functions, but there is a case that can only be overloaded with the element function.
That is: The variable to the left of the binocular operator is a constant, not an Object!!! This is a very important amount.

With respect to overloading of operators, the following experience can be remembered:

1. For monocular operators, it is recommended to select member functions;
2. For operator "=, (),[],->" only as a member function;
3. For operator "+ =,-=,/=,*=,&=,! =,~=,%=,<<=,>>= "recommended overloads for member functions;
4. For other operators, it is recommended that the overload be a friend function.

Then the answer to the following question is obvious:

The "+" in X+y*z is overloaded with member functions, and the "*" UF meta function overload should be written as:?

The answer is: x.operator+ (operator* (y,z))

member function overloading operator and friend function overloading operator from a two-level problem

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.