Is it possible to make Friend function Static???
Static Friend Function
Collapse
X
-
Tags: None
-
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.Comment
-
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.
Code:#include "stdio.h" #include "stdlib.h" class A; class B { public: B(); ~B(); static void SetAiPrivate(int value); static A *pa; }; class A { friend void B::SetAiPrivate(int); public: A(){iPrivate = 0;} ~A(){} void PrintData(){printf("iPrivate = %d\n", iPrivate);} private: int iPrivate; }; A *B::pa; B::B() { pa = new A; } B::~B() { delete pa; } void B::SetAiPrivate(int value) { pa->iPrivate = value; } int main() { B b; B::SetAiPrivate(7); B::pa->PrintData(); }Comment
-
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....
Code:class A; class B { public: B(); static friend void setValue(int val); static A* pa; }; class A { static friend void setValue(int val); int ia; public: A():ia(0){} void display() { cout<<"Data:"<<ia<<endl; } }; B::B() { pa = new A; } A* B::pa = 0; void setValue(int val) { B b; b.pa->ia = val; } int main(int argc, char** argv[]) { B b; setValue(5); B::pa->display(); return 0; }Comment
-
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
Eagerly Waiting for your replyCode:class A; class B { public: B(); static friend void setValue(int val); static A* pa; }; class A { static friend void setValue(int val); int ia; public: A():ia(0){} void display() { cout<<"Data:"<<ia<<endl; } }; B::B() { pa = new A; } A* B::pa = 0; void setValue(int val) { B b; b.pa->ia = val; } int main(int argc, char** argv[]) { B b; setValue(5); B::pa->display(); return 0; }Comment
Comment