Hi. My question is, how do you make a function in another class friend of the class you're in?
I mean that i can make a global function friend of a class, as:
It works if friend_func(,) does not belong to any class, but if I want to make a friend function from another class I can't make it;Code:class X { friend void friend_func(X*, int); private: int i; public: void member_func(int); }; void friend_func(X* xptr, int a) { xptr–>i = a; } /* this has access to X.i */
besides, i wouldn't like to make the whole class friend.
I can't put Y in front of X 'cos i need X instances in Y.Code:class X { friend void Y::friend_func(X*, int); private: int i; public: void member_func(int); //blah blah }; class Y { public: void friend_func(X*, int); // stuff... };
How do i gain access from Y::friend_func(...) to X? It won't work, even if i declare the class at the beginning as
neitherCode:class Y; class X { friend void Y::friend_func(X*, int); }; class Y { void friend_func(X*, int); };
'cause then i get multiple declaration error, and if i don't do it, i get friend_function is not a member of... although it is declared afterwards.Code:class Y {void friend_func(X*,int);}; class X { friend void Y::friend_func(X*, int); // ... stuff... }; class Y { // the rest of Y here };
any hints?
thanks alot![]()




Reply With Quote