Using friend function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    Using friend function

    Hi,
    I have a simple question to ask.

    I was just trying some sample program using friend function.
    The program goes like this

    Code:
    #include<iostream>
    using namespace std;
    
    class B;
    class A
    {
       int _x;
    public:
       A():_x(100){}
      ~A(){}
      friend void funcA(B& b);
    };
    
    class B
    {
       int _y;
    public:
      B():_y(200){}
      ~B(){}
     friend void funcB(A& a);
    };
    
    void funcA(B& b)
    {
       cout<<b._y<<endl;
    }
    
    void funcB(A& a)
    {
      cout<<a._x<<endl;
    }
    
    int main()
    {
      A a;
      B b;
      funcA(b);
      funcB(a);
    
      return 0;
    }
    Now I get some errors which are fortunate, that I cannot access private members declared in respective classes.

    But though I have used friend function why M I getting those errors

    Then I just added these two statements in both the classes

    Code:
    class B;
    class A
    {
       int _x;
    public:
       A():_x(100){}
      ~A(){}
      friend void funcA(B& b);
      friend void funcB(A& a);
    };
    
    class B
    {
       int _y;
    public:
      B():_y(200){}
      ~B(){}
     friend void funcA(B& b);
     friend void funcB(A& a);
    };
    It works fine....
    Can anyone explain more detail about friend functions....
    I'm still not confident about this....

    Thankx in advance
  • dush
    New Member
    • Sep 2006
    • 27

    #2
    Hi

    You've been trying to access private member A::_x from the inside of B class and private member B::_y from the inside of A class. In another words you are passing object of B type by reference to funcA inside of class A (and object of A type to funcB inside of class B)

    funcA (funcB) is global function (not member function) which allows you to access private members of A (B) class from the outside of the class. But it doesnt mean it allows you to access them from inside of another class.

    have a look, I do allowed accessing here:

    Code:
    #include<iostream>
    using namespace std;
    
    class B;
    class A
    {
       int _x;
    public:
       A():_x(100){}
      ~A(){}
      friend void funcA(A& a);
    };
    
    class B
    {
       int _y;
    public:
      B():_y(200){}
      ~B(){}
     friend void funcB(B& b);
    };
    
    void funcA(A& a)
    {
       cout<<a._x<<endl;
    }
    
    void funcB(B& b)
    {
      cout<<b._y<<endl;
    }
    
    int main()
    {
      A a;
      B b;
      funcA(a);
      funcB(b);
    
      return 0;
    }
    In your 2nd example that works can global functions funcA and funcB access private members of both classes because each of these functions is a friend of both classes A and B so private members of A are shared to B class and private members of B are shared to A class through these global functions only.

    If you would like to access private member from inside of each other classes you can make these classes friends and accessing functions declare as classic member functions. Then accessing all the private class members of both classes is allowed inside both of them. In this case you would call accessing functions using the objects ( a.funcA(b), b.funcB(a)) only like I do in next example:
    Code:
    #include<iostream>
    using namespace std;
    
    class B;
    class A
    {
       int _x;
    public:
       A():_x(100){}
      ~A(){}
      void funcA(B& b);
      friend class B;
    };
    
    class B
    {
       int _y;
    public:
      B():_y(200){}
      ~B(){}
     void funcB(A& a);
     friend class A;
    };
    
    void A::funcA(B& b)
    {
       cout<<b._y<<endl;
    }
    
    void B::funcB(A& a)
    {
      cout<<a._x<<endl;
    }
    
    int main()
    {
      A a;
      B b;
      a.funcA(b);
      b.funcB(a);
    
      return 0;
    }
    hope It helped to understand

    anyway, when classes are in friendship it doesnt mean that you can use each other private members in global functions that has friendship only with one of them.

    this would not work:

    Code:
    #include<iostream>
    using namespace std;
    
    class B;
    class A
    {
       int _x;
    public:
       A():_x(100){}
      ~A(){}
      void friend funcA(B& b);
      friend class B;
    };
    
    class B
    {
       int _y;
    public:
      B():_y(200){}
      ~B(){}
     void friend funcB(A& a);
     friend class A;
    };
    
    void funcA(B& b)
    {
       cout<<b._y<<endl;
    }
    
    void funcB(A& a)
    {
      cout<<a._x<<endl;
    }
    
    int main()
    {
      A a;
      B b;
      funcA(b);
      funcB(a);
    
      return 0;
    }

    Comment

    • dush
      New Member
      • Sep 2006
      • 27

      #3
      sorry i made mistake at the begin of my previous post above

      what you tried in first program was not accessing class private members from inside of another class, but it was accessing them from inside of global function. Each of your classes there have a friendship with wrong function.

      exchange the lines 11 and 20 in your program

      Comment

      Working...