By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,908 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,908 IT Pros & Developers. It's quick & easy.

member functions as friends - friends of each other?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 23 '05 #2

P: n/a
yeah I guess so...

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

Thanks

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.