470,874 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,874 developers. It's quick & easy.

Static Friend Function

Is it possible to make Friend function Static???
Sep 22 '06 #1
5 6408
Banfa
9,065 Expert Mod 8TB
Is it possible to make Friend function Static???
Yes, I think without the befinit of context
Sep 22 '06 #2
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
9,065 Expert Mod 8TB
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
vermarajeev
180 100+
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
vermarajeev
180 100+
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.

Similar topics

5 posts views Thread by Naren | last post: by
3 posts views Thread by Marcin Vorbrodt | last post: by
1 post views Thread by Mike - EMAIL IGNORED | last post: by
15 posts views Thread by Samee Zahur | last post: by
2 posts views Thread by asetofsymbols | last post: by
40 posts views Thread by vishnu | last post: by
14 posts views Thread by Jeroen | last post: by
4 posts views Thread by John Doe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.