473,378 Members | 1,518 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,378 software developers and data experts.

Static Friend Function

Is it possible to make Friend function Static???
Sep 22 '06 #1
5 6670
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>;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.