By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Using friend function

100+
P: 180
Hi,
I have a simple question to ask.

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class B;
  5. class A
  6. {
  7.    int _x;
  8. public:
  9.    A():_x(100){}
  10.   ~A(){}
  11.   friend void funcA(B& b);
  12. };
  13.  
  14. class B
  15. {
  16.    int _y;
  17. public:
  18.   B():_y(200){}
  19.   ~B(){}
  20.  friend void funcB(A& a);
  21. };
  22.  
  23. void funcA(B& b)
  24. {
  25.    cout<<b._y<<endl;
  26. }
  27.  
  28. void funcB(A& a)
  29. {
  30.   cout<<a._x<<endl;
  31. }
  32.  
  33. int main()
  34. {
  35.   A a;
  36.   B b;
  37.   funcA(b);
  38.   funcB(a);
  39.  
  40.   return 0;
  41. }
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

Expand|Select|Wrap|Line Numbers
  1. class B;
  2. class A
  3. {
  4.    int _x;
  5. public:
  6.    A():_x(100){}
  7.   ~A(){}
  8.   friend void funcA(B& b);
  9.   friend void funcB(A& a);
  10. };
  11.  
  12. class B
  13. {
  14.    int _y;
  15. public:
  16.   B():_y(200){}
  17.   ~B(){}
  18.  friend void funcA(B& b);
  19.  friend void funcB(A& a);
  20. };  
It works fine....
Can anyone explain more detail about friend functions....
I'm still not confident about this....

Thankx in advance
Sep 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: 27
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:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class B;
  5. class A
  6. {
  7.    int _x;
  8. public:
  9.    A():_x(100){}
  10.   ~A(){}
  11.   friend void funcA(A& a);
  12. };
  13.  
  14. class B
  15. {
  16.    int _y;
  17. public:
  18.   B():_y(200){}
  19.   ~B(){}
  20.  friend void funcB(B& b);
  21. };
  22.  
  23. void funcA(A& a)
  24. {
  25.    cout<<a._x<<endl;
  26. }
  27.  
  28. void funcB(B& b)
  29. {
  30.   cout<<b._y<<endl;
  31. }
  32.  
  33. int main()
  34. {
  35.   A a;
  36.   B b;
  37.   funcA(a);
  38.   funcB(b);
  39.  
  40.   return 0;
  41. }
  42.  
  43.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class B;
  5. class A
  6. {
  7.    int _x;
  8. public:
  9.    A():_x(100){}
  10.   ~A(){}
  11.   void funcA(B& b);
  12.   friend class B;
  13. };
  14.  
  15. class B
  16. {
  17.    int _y;
  18. public:
  19.   B():_y(200){}
  20.   ~B(){}
  21.  void funcB(A& a);
  22.  friend class A;
  23. };
  24.  
  25. void A::funcA(B& b)
  26. {
  27.    cout<<b._y<<endl;
  28. }
  29.  
  30. void B::funcB(A& a)
  31. {
  32.   cout<<a._x<<endl;
  33. }
  34.  
  35. int main()
  36. {
  37.   A a;
  38.   B b;
  39.   a.funcA(b);
  40.   b.funcB(a);
  41.  
  42.   return 0;
  43. }
  44.  
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:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class B;
  5. class A
  6. {
  7.    int _x;
  8. public:
  9.    A():_x(100){}
  10.   ~A(){}
  11.   void friend funcA(B& b);
  12.   friend class B;
  13. };
  14.  
  15. class B
  16. {
  17.    int _y;
  18. public:
  19.   B():_y(200){}
  20.   ~B(){}
  21.  void friend funcB(A& a);
  22.  friend class A;
  23. };
  24.  
  25. void funcA(B& b)
  26. {
  27.    cout<<b._y<<endl;
  28. }
  29.  
  30. void funcB(A& a)
  31. {
  32.   cout<<a._x<<endl;
  33. }
  34.  
  35. int main()
  36. {
  37.   A a;
  38.   B b;
  39.   funcA(b);
  40.   funcB(a);
  41.  
  42.   return 0;
  43. }
  44.  
Sep 19 '06 #2

P: 27
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
Sep 20 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.