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

Static Friend Function

P: 10
Is it possible to make Friend function Static???
Sep 22 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
Is it possible to make Friend function Static???
Yes, I think without the befinit of context
Sep 22 '06 #2

P: 5
No we cant make the friend function as static, bcoz Static member function can used only the static data member and friend function can use the private data member not the static data members. The static data member is having global scope but visible only within the class.
Sep 23 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Nobody said it was a member function, only that it was static.

A static member function can only access static data members of it's own class, this does not preclude it from accessing any data members of another class and would not stop it accessing private members where it declared as a firend.

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3.  
  4. class A;
  5.  
  6. class B
  7. {
  8. public:
  9.     B();
  10.     ~B();
  11.  
  12.     static void SetAiPrivate(int value);
  13.     static A *pa;
  14. };
  15.  
  16. class A
  17. {
  18. friend void B::SetAiPrivate(int);
  19.  
  20. public:
  21.     A(){iPrivate = 0;}
  22.     ~A(){}
  23.     void PrintData(){printf("iPrivate = %d\n", iPrivate);}
  24.  
  25. private:
  26.     int iPrivate;
  27. };
  28.  
  29.  
  30. A *B::pa;
  31.  
  32. B::B()
  33. {
  34.     pa = new A;
  35. }
  36.  
  37. B::~B()
  38. {
  39.     delete pa;
  40. }
  41.  
  42. void B::SetAiPrivate(int value)
  43. {
  44.     pa->iPrivate = value;
  45. }
  46.  
  47. int main()
  48. {
  49.     B b;
  50.  
  51.     B::SetAiPrivate(7);
  52.     B::pa->PrintData();
  53. }
  54.  
Sep 23 '06 #4

100+
P: 180
Hi Banfa,
I'm glad by your solution..Can you please explain me your program above.
Also I have made some modification here. Please explain me the difference. I'm still confused about friend functions.....

I need your help to understand the concept....

Expand|Select|Wrap|Line Numbers
  1. class A;
  2.  
  3. class B
  4. {
  5. public:
  6.     B();    
  7.     static friend void setValue(int val);
  8.     static A* pa;
  9. };
  10.  
  11. class A
  12. {
  13.     static friend void setValue(int val);
  14.     int ia;
  15. public:
  16.     A():ia(0){}  
  17.     void display()
  18.     {
  19.         cout<<"Data:"<<ia<<endl;
  20.     }
  21. };
  22.  
  23. B::B()
  24. {
  25.     pa = new A;
  26. }
  27.  
  28. A* B::pa = 0;
  29.  
  30. void setValue(int val)
  31. {
  32.     B b;
  33.     b.pa->ia = val;
  34. }
  35.  
  36. int main(int argc, char** argv[])
  37. {
  38.     B b;    
  39.     setValue(5);
  40.     B::pa->display();
  41.     return 0;
  42. }
Sep 25 '06 #5

100+
P: 180
Hi banfa,
I'm glad by your solution....

Can you elaborate about what you have written the code above...

Also I have done some modifications in your program....
Can you explain me the difference...
I'm confused by the term friend and needs to clear about that

Expand|Select|Wrap|Line Numbers
  1. class A;
  2.  
  3. class B
  4. {
  5. public:
  6.     B();    
  7.     static friend void setValue(int val);
  8.     static A* pa;
  9. };
  10.  
  11. class A
  12. {
  13.     static friend void setValue(int val);
  14.     int ia;
  15. public:
  16.     A():ia(0){}  
  17.     void display()
  18.     {
  19.         cout<<"Data:"<<ia<<endl;
  20.     }
  21. };
  22.  
  23. B::B()
  24. {
  25.     pa = new A;
  26. }
  27.  
  28. A* B::pa = 0;
  29.  
  30. void setValue(int val)
  31. {
  32.     B b;
  33.     b.pa->ia = val;
  34. }
  35.  
  36. int main(int argc, char** argv[])
  37. {
  38.     B b;    
  39.     setValue(5);
  40.     B::pa->display();
  41.     return 0;
  42. }
Eagerly Waiting for your reply
Sep 25 '06 #6

Post your reply

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