member functions as friends - friends of each other?

B

bipod.rafique

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.

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?

Thanks
Bipod
 
J

Jonathan Mcdougall

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
 
B

bipod.rafique

yeah I guess so...

As an alternative, I can make the whole class as friend and vice versa.
That works fine.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top