C ++ operator is defined as a friend function.

Source: Internet
Author: User

 

 
Because I encountered a problem when I wrote a small program myself, it is not acceptable for some operator to be defined as a member function, but it is OK to define it as a friend, and some do not understand it, it is somewhat enlightening to see an article written by redsnow. The original Article link is http://blog.chinaunix.net/u/24250/showart_182419.html.
 
The operator function defined as friend in class has the following considerations: 1. the friend function is made public, while the class method is an object. In some cases, calling is not convenient. for some operator functions that require two parameters, defining friend is more convenient. In the following example, operator <3. all class methods must have matched left value types for calling, while friend does not need to. This function can be called as long as it can be implicitly converted to the current type, therefore, the following constructor is not defined as explicit. You can perform implicit conversion to perform operations between different types. The following example shows the benefits of defining a friend function.
# Include <iostream. h>
Class Point
{
Int X;
Int y;
Public:
Point (INT vx = 0) {x = VX; y = 0 ;}
Point (int vx, int Vy): X (VX), y (Vy ){}
Friend point operator + (point P1, point P2 );
Friend ostream & operator <(ostream & output, point & P1 );
};

Point operator + (point P1, point P2)
{
Point P;
P. x = p1.x + p2.x;
P. Y = p1.y + p2.y;
Return P;
}


Ostream & operator <(ostream & output, point & P1)
{
Output <p1.x <'+' <p1.y <'I' <Endl;
Return output;
}

Int main ()
{
Point P1 (1, 2), P2 (5, 6 );
Point P3, P4;
P3 = p1 + P2;
P4 = 1 + P2; // if it is defined as class method, compilation will fail! Class provides the corresponding constructor and does not explain
Cout <P1 <P2;
Cout <P3 <P4;
Return 0;
} There are some inspirations, but because there is no electricity in the book, we cannot find out the reason for the original program for the time being. First, record it for storage. thks for redsnow will be modified later... The difference between the choice of friend and class-defined operator is as follows. However, member function operators and member function operators have their own characteristics:
(1) In general, it is best to reload a single object operator as a member function of the class, and a binary operator as a friend function of the class. (2) The following binary operators cannot be overloaded as class friend functions: =, (), [],->. (3) A type conversion function can only be defined as a member function of a class, rather than a friend function of a class. (4) If an operator needs to modify the object state, it is better to select reload as a member function. (5) If the required operands (especially the first operand) of the operator want to have implicit type conversion, you can only use the youyuan function. (6) When an operator function is a member function, the leftmost operand (or only the leftmost operand) it must be a class object of an operator class (or a reference to this class object ). If the operand on the left must be an object of different classes or an internal type, this operator function must be implemented as a friend function. (7) When the overload operator is required to be interchangeable, select the overload as a friend function. Add yourself: The friend overload of binary operators can limit the first parameter to the const type, which cannot be done by member functions, especially for calls to some template classes, these limits are often required. Here are my code and problems. Class classa {
Friend ostream & operator <(ostream & OS, const classa & CIA ){
OS <"this is the object of classa" <Endl;
Return OS;
} Friend bool operator <(const classa & CLB, const classa & linoleic ){
Cout <"friend" <Endl;
Return true;
}
}; Map <classa, classa> mapclass;
Classa cl11, cl22, cl33;
Cout <cl33;
Mapclass [cl11] = cl22; // compare the keywords in map. The input is the key parameter of Const. Therefore, it is not allowed to pass in non-const overload operators, therefore, the overload as a member function is incorrect and cannot be run.

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.