Some summaries of friend functions in C + + _c language

Source: Internet
Author: User

1. A simple introduction to the friend function

1.1 Why to make the Ufida function

Reduce system overhead and improve efficiency when implementing data sharing between classes. If a function in Class A accesses a member of Class B (for example, an implementation of a smart pointer Class), then the function in Class A is a friend function of Class B. Specifically: to make the member functions of other classes directly access the private variables of the class. That is, allow the outside class or function to access the class's private variables and protect the variables so that two classes share the same function.

In fact, there are approximately two situations in which the friend function needs to be used: (1) Some occasions in which operator overloading requires the use of friends. (2) Two classes to share data.

1.2 Advantages and disadvantages of using friend functions

1.2.1 Advantages: Can improve efficiency, simple and clear expression.

1.2.2 Disadvantage: Friend function broken ring encapsulation mechanism, as far as possible not to use member functions, unless the case only to make Ufida function.

2. Use of friend function

2.1 Parameters for the friend function:

Because the friend function does not have the this pointer, there are three cases of arguments:

2.1.1 To access a non-static member, the object is required to make arguments;

2.1.2 When you want to access a static member or global variable, you do not need an object to do the argument;

2.1.3 If the object that makes the parameter is a global object, the object does not need to be the parameter;

2.2 Location of the friend function

Because a friend function is a function outside of a class, its declaration can be placed in a private or public segment of a class without distinction.

2.3 Call to a friend function

Can directly adjust the function of the UF, do not need to pass the object or pointer

2.4 Categories of friend functions:

Depending on the source of this function, there are three different ways:

2.4.1 Common function Friend function

2.4.1.1 Purpose: To enable ordinary functions to access friends of a class

2.4.1.2 Syntax:

Statement: Friend + normal function declaration

Implementation location: Can be outside the class or in a class

Implementation code: Same as normal function

Call: Similar to normal function, call directly

2.4.1.3 Code:

Copy Code code as follows:

Class INTEGER

{

friend void Print (const integer& obj);//Declaration friend function

};

void Print (const integer& obj)

{

function body

}

void Main ()

{

INTEGER obj;

Print (obj);//Call directly

}


All member functions of 2.4.2 Class Y are class X friend functions-friend class

2.4.2.1 Purpose: Use a single declaration to make all functions of the Y Class A friend of Class X, which provides a way to collaborate between classes, so that the object of class Y can have the functions of Class X and Class Y.

2.4.2.2 Syntax:

Declaration location: Public private can be, often written as private (the class as a variable)

Statement: Friend + class name (not object OH)

2.4.2.3 Code:

Copy Code code as follows:

Class girl;

Class Boy

{

Public

void disp (girl &);

};

void boy::d ISP (Girl &x)//Function disp () is a member function of Class boy, but also a friend function of class girl

{

cout<< "Girl ' s name is:" <<x.name<<, Age: "<<x.age<<endl;//with friends, in boy member function disp, Direct access to girl's private variables with the help of Girl objects

}

Class Girl

{

Private

Char *name;

int age;

Friend Boy; Declaring class boy is a friend of class girl

};


The main function does not write the same as the normal call.

A member function of 2.4.3 Class Y is a friend function of class X

2.4.3.1 purpose: To make a member function of Class y a friend of Class X, specifically: In this member function of Class Y, with the help of parameter x, the private variable of x can be directly

2.4.3.2 Syntax:

Declaration position: Declaration in public (itself as a function)

Declaration: The Declaration of Friend + member function

Call: First define Y's object y---use y to call your own member function---Use the friend mechanism in your own member functions

2.4.3.3 Code:

Implementations and similarities in the implementation code and 2.4.2.3 are only friends when setting up friend void boy::d ISP (Girl &);

Summary: In fact, some operators of the overload implementation is also to be implemented outside the class, so often, the declaration as a friend of the class is a must drop.

4. The difference between a friend function and a member function of a class

The 4.1 member function has this pointer, and the friend function has no this pointer.

4.2 Friend function can not be inherited, just like the father's friend is not necessarily a son's friend.

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.