[C / C ++ function friend class notes]

Recently learned friend, there are three usage:

1 friend function

2 Tomomoto类

3 friend class function

I found the use friend class function than the two usage to be strict, do not follow the format will write all kinds of mistakes, take two classes are open to write, is divided into four steps.

Step:   class A; // with a friend class declaration class function 
Step:   class B {
                 public :
                 int getNum (A); 
           }; // declare classes and functions friend class containing function - - a with the foregoing                     
third step:   class a {
                 int NUM;
                 public : 
                Friend int B :: getNum (a); 
           }; // the class a finish with the foregoing --- B and getNum (a) 
step Four:   int B :: getNum (A A) {
                 return a.num; 
           } //The friend class function --- finished with the previous num

 

Guess you like

Origin www.cnblogs.com/LPworld/p/11329470.html