C ++ youyuan and c youyuan Functions
URL: http://www.cnblogs.com/archimedes/p/cpp-friend.html
Class advance declaration
The prototype Declaration of the recall Function
# Include <iostream> using std: cout; using std: endl; void test (); int main () {test (); return 0;} void test () {cout <"advance declaration of function! "<Endl ;}
Classes and functions are similar, and sometimes they need to be declared in advance.
Class B; // declare class A {// class B} in advance; int main () {void ff (B &); // RIGHT can declare reference B * p_ B = NULL; // RIGHT can define a pointer! // B bb; // ERROR cannot define the object system ("PAUSE"); return 0 ;}class B {}; void ff (B &){}
Note:
Declare objects of this class in advance
Only the pointer or reference of this class can be defined.
Why do we introduce youyuan?
Generally, non-public members in a class cannot be directly accessed by non-member functions of the class! If we declare the function as a friend of this class, this function will be able to access non-public members of this class!
Non-member functions
Concept: declares an out-of-class function (top-level function) as a friend function.
Declaration method:
Name of the friend type function (table of parameters );
Declare the function with friend in the class!
#include <iostream>#include <string>using namespace std;class Time{public: Time(int aH=0, int aM=0, int aS=0); friend void display(Time&);private: int hour; int minute; int second;};Time::Time(int aH, int aM, int aS){ hour=aH; minute=aM; second=aS;}void display(Time &t){ cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;}int main(){ Time t(12,30,54); display(t); system("PAUSE"); return 0;}
Note:
① The friend functions should be declared in the class body, and the friend function is only used for declaration and only appears once.
② The call method is the same as that of a common global function.
③ The declaration statement of the youyuan global function is the declaration statement of the function.
④ Youyuan global functions can also be defined in the class. At this time, the compiling environment still regards them as global functions, and the definition is equivalent to declaration without prior declaration.
⑤ A global function can be declared as a friend of multiple classes.
⑥ Multiple global functions can be declared as friends of a class.
Member Functions
Concept: A member function of a class can be declared as a friend of another class.
Declaration method:
Friend type Class Name: function name (form parameter table );
# Include <iostream> # include <string> using namespace std; class Atest; // declare class Btest {public: Btest (int aX = 4) in advance: m_dVal (aX) {} void display (Atest & aVal); private: double m_dVal;}; class Atest {public: Atest (int aX = 3): m_iVal (aX) {} friend void Btest:: display (Atest & aVal); private: int m_iVal;}; void Btest: display (Atest & aVal) {cout <"Atest. m_iVal = "<aVal. m_iVal <endl; cout <"Btest. m_dVal = "<m_dVal <endl;} int main () {Atest testa; Btest testb; testb. display (testa); getchar (); return 0 ;}Youyuan class
Why do we introduce a friend class?
If A member function in Class A wants to be A non-public member of Class B, we can declare A as A membership class of Class B.
Declaration method:
Friend class name;
Class Date; class Person {friend Date; // any position in the person class}; class Date {};
Note:
① Youyuan is not passed!
② One-way relationship between friends and Yuan!
③ The relationship between friends and Yuan destroys the principle of information hiding, but the relationship between friends and Yuan also provides a method for data sharing.
④ Youyuan is not subject to access restrictions.