bipod.rafi...@gmail.com wrote:
> Hello All,
>
> I have the following two classes:
>
> class testb{
> private:
> int b;
> public:
> void friend_of_testa();
> };
>
> class testa{
> friend void testb::friend_of_testa();
> private:
> int a;
> };
>
> Here, testb's member function friend_of_testa() is a friend of testa.
> So private member of testa (say int a), is accessible from testb's
> friend_of_testa. This is all good.
>
> But what if I wanted a member function of testa be a friend of class
> testb as well? like the following:
>
> class testa; //forward reference
>
> class testb{
> friend void testa::friend_of_testb();
> private:
> int b;
> public:
> void friend_of_testa();
> };
>
> class testa{
> friend void testb::friend_of_testa();
>
> private:
> int a;
> public:
> void friend_of_testb();
> };
>
> This is not possible as testa's friend_of_testb() is unknown to the
> compiler at the time of testb's class declaration.
Of course.
> I know I would need a forward reference for testa before testb can use
> it. But how do make testa's friend_of_testb() a forword reference for
> the above to work?
You cannot, because there is no way to forward-declare member
functions. The only thing you can do is :
class testa;
class testb
{
friend testa;
public:
void f();
};
class testa
{
friend testb::testa;
public:
void g();
};
testb::f() is a friend of testa and testa is a friend of testb. That's
the closest you can get.
Jonathan
|