473,511 Members | 16,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Friend Function

10 New Member
Is it possible to make Friend function Static???
Sep 22 '06 #1
5 6679
Banfa
9,065 Recognized Expert Moderator Expert
Is it possible to make Friend function Static???
Yes, I think without the befinit of context
Sep 22 '06 #2
deepakpb
5 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 New Member
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
9373
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
28173
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
3769
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
1944
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
6556
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
1377
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
3182
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
1881
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
2551
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
2812
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
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.