Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member functions as friends - friends of each other?

Reply
Thread Tools

member functions as friends - friends of each other?

 
 
bipod.rafique@gmail.com
Guest
Posts: n/a
 
      07-16-2005
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

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-16-2005
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

 
Reply With Quote
 
 
 
 
bipod.rafique@gmail.com
Guest
Posts: n/a
 
      07-16-2005
yeah I guess so...

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

Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
private member functions as friends Dan Smithers C++ 3 07-09-2008 10:39 AM
warning: member functions are implicitly friends of their class Guenther Sohler C++ 6 10-07-2006 09:07 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57