473,222 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

Static Friend Function

Is it possible to make Friend function Static???
Sep 22 '06 #1
5 6660
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

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

Similar topics

5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
1
by: Mike - EMAIL IGNORED | last post by:
Are static friend functions allowed? I tried this today with gcc-3.2 without success. The function was not a class member. The friend statement was interpreted as meaning "extern", and this was...
5
by: JustSomeGuy | last post by:
I have a class with a private data member and I want to access it from a friend function, but Visual Studio 2003 .NET won't let me. class MyClass { private: static int level; public: friend...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
2
by: asetofsymbols | last post by:
Hi i'm new to c++ i have the class num for big unsigned numbers it seems work ok until now and i want extend the numbers to big-signed and big-float typedef struct{ unsigned len; unsigned* ...
40
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
1
by: Alex Vinokur | last post by:
I want static functions of classes Uam and Bar to be friends of class Foo. There is no problem with Uam::uam(). But class Bar has a member of Foo type: member Bar::m_foo, so one can't put Bar...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
4
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.