C + +-friend function

Source: Internet
Author: User

Friend function

1, why do you want to introduce friend function?

Reduce system overhead and increase efficiency when data sharing between classes is implemented

Specifically, in order for the member functions of other classes to access the private variables of the class directly, that is, allow the outside class or function to access the class's private and protection variables, so that two classes share the same function

Advantages: Can improve efficiency, express simple, clear

Disadvantage: The friend function breaks the encapsulation mechanism, tries not to use the member function, unless the unavoidable situation makes the UF meta-function.

2. When to use friend function?

1) operator overloading requires the use of friends for some occasions.

2) Two classes to share the data

3, how to use friend function?

Arguments to the friend function:

Because the friend function does not have this pointer, there are three conditions for the parameter:

1, to access non-static members, the object is required to do parameters;--common (friend function often contains parameters)

2. When you want to access a static member or global variable, you do not need the object to do the argument

3, if the object that makes the parameter is a global object, then the object does not need to do parameters

The location of the friend function:

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

Call of the friend function:

You can directly adjust the cell function, do not need to pass through the object or pointer

Classification of friend functions

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

1, Common function friend function:

A) Purpose: to enable ordinary functions to access the friend of the class

b) Syntax: Declaration location: Public private, often written as public

Disclaimer: Friend + common function declaration

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

Implementation code: Same as normal function (no friend and class::)

Call: Similar to normal function, call directly

c) Code:

classinteger{Private:    intnum; Public: FriendvoidPrint (Constinteger& obj);//declaring friend functions};voidPrint (Constinteger& obj)//do not use friend and class::{    //function Body}
voidMain () {INTEGER obj; Print (obj);//Call directly}

  

2. All member functions of Class Y are class X friend functions-friend class

A) Purpose: Use a single declaration to make all functions of the Y Class A friend of Class X

It provides a way to collaborate between classes so that objects of class Y can have the functions of Class X and Class Y

Specifically:

premise: A is a friend of B (= "A" member function can access all members in B, including private members and public members--old forgetting)

Then: In a, with class B, you can use ~b directly. Private variable ~ form access to private variable

b) Syntax: Declaration location: Public private can, often write as private (the class as a variable)

Disclaimer: friend + class name ---Not an object.

Call:

c) Code:

classgirl;classboy{Private:    Char*name; intAge ;  Public: Boy (); voidDisp (Girl &);  }; voidBoy::d ISP (Girl &x)//The function disp () is the member function of the class boy, and also the friend function of the class girl.{cout<<"Boy's name is:"<<name<<", Age:"<<age<<endl;//Normally, boy's member function disp directly accesses the boy's private variablecout<<"Girl ' s name is:"<<x.name<<", Age:"<<x.age<<Endl; //with the help of friends, in the boy's member function disp, with the help of Girl object, direct access to girl's private variables//under normal circumstances, only the private variables of girl are allowed in the member functions of Girl}classgirl{Private:Char*name; intAge ;   Friend Boy; //declaration class boy is a friend of class girl Public: Girl ();  }; voidMain () {Boy B;      Girl G;  B.disp (g); //B calls its own member function, but with the G parameter, the friend mechanism is embodied in the function disp}

3. A member function of Class Y is a friend function of class X

A) Objective: to make a member function of Class y a friend of class X

In particular: In this member function of Class Y, with the help of parameter x, you can use X directly. Private variables in the form of access to private variables

b) Syntax: Declaration location: Declared in public (itself as a function)

Declaration:Friend + declaration of member function

Call: Define Y's Object y---use y to call your own member function---Use the friend mechanism in your own member function

c) Code:

classgirl;classboy{Private:    Char*name; intAge ;  Public: Boy (); voidDisp (Girl &);    }; classgirl{Private:    Char*name; intAge ;  Public: Girl (Char*n,intA); FriendvoidBoy::d ISP (Girl &);//declaring a member of the Class boy function disp () is a friend function of class girl}; voidBoy::d ISP (Girl &x) {cout<<"Boy's name is:"<<name<<", Age:"<<age<<endl;//Access your Own (boy) object members and access your own private variables directlycout<<"Girl ' s name is:"<<x.name<<", Age:"<<x.age<<Endl; //with the help of friends, in the boy's member function disp, with the help of Girl object, direct access to girl's private variables//under normal circumstances, only the private variables of girl are allowed in the member functions of Girl}  voidMain () {boy B ();      Girl g (); B.disp (g);
}

4. Use friend operator<< (overload of << operator) in template class

A) How to use:

Declare in the template class:

operator<< <> (ostream& cout,const mgraph<vextype,arctype>& G);

Defined in the template class:

template<class Vextype,class arctype>ostreamoperator<< (ostream & cout,const mgraph<vextype,arctype>& G) {    // function definition }

b) Note:

To declare a function as a non-template function:

operator<< (ostream& cout,const mgraph& G);

    To declare a function as a template function:

operator<< <> (ostream& cout,const mgraph<vextype,arctype>& G);

  Or:

operator<< <VexType,ArcType> (ostream& cout,const mgraph<vextype,arctype> & G);

Description
Add operator<< <> to the function declaration: The operator<< function is defined as a function template, and the function template is declared as a friend of the class template.
The actual declaration function: Here the template parameters can be omitted, but the angle brackets can not be omitted

operator<< <VexType,ArcType> (ostream& cout,const mgraph<vextype,arctype> & G);

5. The difference between a friend function and a member function of a class: The member function has this pointer, and the friend function does not have the this pointer.

6, Memory: A is a friend of B "=" A is B's friends "=" with the help of B object, in a can be directly through B. Member variable (can be public or private variable) Access B

C + +-friend function

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.