1, why to introduce friend function: in the implementation of data sharing between classes, reduce system overhead, improve efficiency
Specifically: To enable member functions of other classes to access Private variables of the class directly
That is: allow outside classes or functions 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
Categories 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:
Class INTEGER { private: int num; Public: friend void Print (const integer& obj);//Declaration friend function }; void Print (const integer& obj)//Do not use friend and class:: { //function body } void Main () { INTEGER obj ; Print (obj);//Direct Call }
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:
class girl; Class Boy {Private:char *name; int age; Public:boy (); void disp (girl &); }; void boy::d ISP (Girl &x)//Function disp () is a member function of Class boy, and also a friend function of class girl {cout<< "boy's name is:" <<NAME&L t;< ", Age:" <<age<<endl;//normal situation, boy's member function disp directly accesses the boy's private variable cout<< "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 variable//Normally, only allow access to the girl private variable in the Girl member function} class girl {Private:char *name; int age; Friend Boy; Declaration class boy is a friend of the class Girl Public:girl (); }; void Main () {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:
Class girl; Class Boy { private: char *name; int age; Public: Boy (); void disp (girl &); }; Class Girl { private: char *name; int age; Public: Girl (char *n,int A); friend void boy::d ISP (Girl &); Declares that the member function of the class Boy disp () is a friend function of class girl }; void boy: The:d ISP (girl &x) { cout<< "boy's name is:" <<name<< ", Age:" <<age<< Endl; Access your Own (boy) object members and access your private variables directly cout<< "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 variable //Normally, only the girl private variable is allowed in the girl member function } void Main () {boy B (); Girl g (); B.disp (g); }
4.using friend operator<< (overloading of the << operator) in a template class
A) How to use:
Declare in the template class:
Friend ostream& operator<< <> (ostream& cout,const mgraph<vextype,arctype>& G);
Defined in the template class:
Template<class vextype,class arctype> ostream& operator<< (ostream& cout,const MGraph< vextype,arctype>& G) { //function definition }
b) Note:
To declare a function as a non-template function:
Friend ostream& operator<< (ostream& cout,const mgraph& G);
To declare a function as a template function:
Friend ostream& operator<< <> (ostream& cout,const mgraph<vextype,arctype>& G);
Or:
Friend ostream& 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
Friend ostream& 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
A summary of C + + friend functions