Specify a class member function as a friend of another class?

后端 未结 7 778
小蘑菇
小蘑菇 2020-12-08 21:43

According to the C++ Primer book, the author mentioned that We can specify a class member function as a friend of another class, instead of the entire class (page 634).

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 22:09

    When the compiler starts compiling the code( usually from top ) and it encounters this line:

    friend void B::fB(A& a);
    
    1. at this point, compiler has no idea about type info of B so it throws an error ( 'B' : is not a class or namespace name ).
    2. by forward declaration of class B, compiler knows about type of B is Class in advance to its actual declaration with all members.

    3. run below code after forward declaration of class B.

    ///////////////

    class B;
    class A
    {
    public:
        friend void B::fB(A& a); 
        void fA(){};
    };
    class B
    {
    public:
        void fB(A& a){};
        void fB2(A& a){};
    };
    

    Still error !!!

    because forward declaration is just a declaration of an identifier for which the programmer has not yet given a complete definition. so compiler needs full definition of B before class A.

    Note: class A definition dependents on type of B and also definition of B (i.e B::fB) so that forward declaration alone can not resolve, complete definition of class B needs to define before class A.

    4 run this code

    ////////

    class B
    {
    public:
        void fB(A& a){};
        void fB2(A& a){};
    };
    class A
    {
    public:
        friend void B::fB(A& a); 
        void fA(){}
    };
    

    Still error !!!

    because class B member functions fB & fB2 having arguments of type A but compiler has no idea about type info of A so by forward declaration of class A, we can let compiler knows about type info of A. Note: class B definition only dependent on type of A not the members of A so that forward declaration of A resolve step 4.

    1. final code

    ////////////////////////

    class A;  // forward declaration of A needed by B
    class B
    {
    public:
        void fB(A& a);
    };
    
    class A
    {
        int i;
    public:
        friend void fA(A& a);    //specifying function fA as a friend of A, fA is not member function of A
        friend void B::fB(A& a); //specifying B class member function fB as a friend of A
    };
    
    // fA is Friend function of A
    void fA(A& a)
    {
        a.i  = 11; // accessing and modifying Class A private member i
        cout<

    6 Exercise:

    // Cyclic dependency 
    #include
    using namespace std;
    
    class A;
    
    class B
    {
    public:
        void fB(A& a);
        friend void A::fA(B& b); //specifying class A's member function fA as a friend of B
    };
    
    class A
    {
        int i;
    public:
        void fA(B& b);  
        friend void B::fB(A& a); //specifying class B's member function fB as a friend of A
    };
    
    int main()
    {
        return 0;
    }
    

提交回复
热议问题