Summary of C ++ friend usage

Source: Internet
Author: User

Summary of C ++ friend usage
The membership mechanism in C ++ allows non-public members of the class to be accessed by a class or function. The membership function of the class can be divided into three types: common non-member functions as friends, and member functions of the class as friends, class as a friend.
The contents of youyuan include the statement of youyuan and the definition of youyuan. By default, the declaration of youyuan is extern. That is to say, the scope of youyuan class or youyuan function has been extended to the scope that contains this class definition. Therefore, even if we define youyuan function within the class, it does not matter.
2. normal non-member functions such as youyuan are usually operators, such as input and output operators. The example is as follows:

//OpeClass.h#pragma onceclass OpeClass{friend int func(const OpeClass xx);public:OpeClass(void);OpeClass(int x,int y);~OpeClass(void);private:int width;int height;};
//OpeClass.cpp#include "OpeClass.h"OpeClass::OpeClass(void){width = 50;height = 50;}OpeClass::OpeClass(int x,int y):width(x),height(y){}OpeClass::~OpeClass(void){}int func(const OpeClass xx){return xx.height * xx.width;}


// Main. cpp # include "OpeClass. h" # include
 
  
Using namespace std; void main () {OpeClass XO; cout <
  
   
As a friend, you must note the dependency between the friend class and the original class. If the function defined in the friend class uses the private variable of the original class, in this case, you need to include the header file of the original class definition in the file of the object definition.
   
However, in the definition of the original class (including the class declared by the youyuan class), you do not need to include the header file of the youyuan class or declare the youyuan class before the class definition, because the declaration of the object meta class itself is a declaration (it specifies that the object meta class can be found outside the class), the example program is as follows:
// A. h # pragma once # include
    
     
Using namespace std; class A {friend class B; public :~ A (void); static void func () {cout <"This is in A" <
     
      
//A.cpp#include "A.h"const A A::Test = A();A::~A(void){}

//B.h#pragma once#include "C.h"class B{public:B(void);~B(void);void func(C& c);};

// B. cpp # include "B. h" # include "A. h" # include "C. h" # include
       
        
Using namespace std; B: B (void) {} B ::~ B (void) {} void B: func (C & c) {cout <"This is in B" <
        
         
//C.h#pragma onceclass A;class C{public:C(void);~C(void);void func(const A& a);};

// C. cpp # include "C. h" # include
          
           
Using namespace std; C: C (void) {} C ::~ C (void) {} void C: func (const A & a) {cout <"This is in C" <
           
            
//main.cpp#include "A.h"#include "B.h"#include "C.h"#include 
             
              using namespace std;void main(){B b;C c;b.func(c);system("pause");}
             
Class 4 member functions as friend Functions

This is a little complicated. Because you want to use a class member function as a friend, you must first define a class that contains a friend function when declaring a friend function, however, when defining a friend function, you must define the original class in advance. The general practice is to first define a class that contains a friend function, and then define the original class. This order cannot be messy. (If it is a friend Meta class, this is not required) as shown below:

//B.h#pragma onceclass A;class B{public:B(void);~B(void);int func(A xx);};

//A.h#pragma once#include "B.h"class A{friend int B::func(A xx);public:A(void):mx(20),my(30){}~A(void){}private:int mx;int my;};

//B.cpp#include "B.h"#include "A.h"B::B(void){}B::~B(void){}int B::func(A xx){return xx.mx * xx.my;}

// Main. cpp # include "A. h" # include "B. h" # include
             
              
Using namespace std; void main () {A a; B B; cout <
              
               
5 friends do not have mutual, only have a single

If Class B is A friend of Class A, Class A is not necessarily A friend of class B. It depends on whether there is A corresponding declaration in the class.

6. youyuan cannot be inherited. B is the member class of A, C is the Child class of B, and C is the member of, C is B's friend, and C is A's friend
8. You don't have to pay attention to the mutual membership category. The following is an instance. Class A and Class B are mutual membership.
//A.h#pragma onceclass A{friend class B;public:A(void);~A(void);int funa(B& b);private:int mx;int my;};

//A.cpp#include "A.h"#include "B.h"A::A(void){mx = 10;my = 10;}A::~A(void){}int A::funa(B& b){return b.mb * b.mc;}

//B.h#pragma onceclass B{friend class A;public:B(void);~B(void);int funb(A& a);private:int mb;int mc;};

//B.cpp#include "B.h"#include "A.h"B::B(void){mb = 20;mc = 20;}B::~B(void){}int B::funb(A& a){return a.mx *a.my;}

// Main. cpp # include "A. h" # include "B. h" # include
                
                 
Using namespace std; void main () {A a; B B; cout <
                 
9 If You Want To specify that both classes have member functions as the Peer's friends, 2nd classes must be friends of the first class.
//A.h#pragma once// class B is a friend class of Aclass A{friend class B;public:A(void):ma(10),mb(20){}~A(void){}int funa(B& b);private:intma;intmb;};

//B.h#pragma once#include "A.h"// A's function funa is a friend function of Bclass B{friend int A::funa(B& b);public:B(void);~B(void);int funb(A& a);int func(A& a);private:int mx;int my;};

//A.cpp#include "A.h"#include "B.h"int A::funa(B& b){return  b.mx * b.my;}

//B.cpp#include "B.h"B::B(void):mx(12),my(15){}B::~B(void){}int B::funb(A& a){return a.ma + a.mb;}int B::func(A& a){return a.ma * a.mb;}

//main.cpp#include "A.h"#include "B.h"#include 
                  
                   using namespace std;void main(){A a;B b;cout<
                   

Related Article

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.