470,833 Members | 1,397 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

"friendship" question

hi all,

I have 2 classes, A and B. Each class has a member function that needs to
access an element of the other class. Here's the code:

#include <iostream>
using namespace std;

class B;

class A
{ public:
void AFunction(B& arg_b);
friend void B::BFunction(B&); //allow BFunction to access avar
private:
int avar;
};

class B
{ public:
void BFunction(A& arg_a);
friend void A::AFunction(B&); //allow AFunction to access bvar
private:
int bvar;
};

void A::AFunction(B& arg_b) { cout << arg_b.bvar << endl; }
void B::BFunction(A& arg_a) { cout << arg_a.avar << endl; }

int main()
{ A MyAObject;
B MyBObject;
}

This doesn't compile because B::BFunction(B&) is declared a friend before B
is defined. But swapping classes A and B just makes A::AFunction the
problem...

Is there a way to do this without making the whole classes friends of each
other?

Thanks in advance,

Thomas
Apr 14 '06 #1
4 1578
Thomas wrote:
hi all,

I have 2 classes, A and B. Each class has a member function that needs to
access an element of the other class. Here's the code:

#include <iostream>
using namespace std;

class B;

class A
{ public:
void AFunction(B& arg_b);
friend void B::BFunction(B&); //allow BFunction to access avar
private:
int avar;
};

class B
{ public:
void BFunction(A& arg_a);
friend void A::AFunction(B&); //allow AFunction to access bvar
private:
int bvar;
};

void A::AFunction(B& arg_b) { cout << arg_b.bvar << endl; }
void B::BFunction(A& arg_a) { cout << arg_a.avar << endl; }

int main()
{ A MyAObject;
B MyBObject;
}

This doesn't compile because B::BFunction(B&) is declared a friend before B
is defined. But swapping classes A and B just makes A::AFunction the
problem...

Is there a way to do this without making the whole classes friends of each
other?


You're pretty much toast. However, either CUJ or DDJ had an article on
"attorney" classes, which basically functioned as friend proxies.
Apr 14 '06 #2

Followup to the 'Attorney class' idea.

See the January 2006 DDJ (or CUJ, it's still not clear). "Friendship and
the Attorney-Client Idiom."

http://www.ddj.com/documents/s=10011...601bolton.html
Apr 14 '06 #3
am Freitag, 14. April 2006 20:06 schrieb red floyd:
Thomas wrote:
[snipped]

You're pretty much toast. However, either CUJ or DDJ had an article on
"attorney" classes, which basically functioned as friend proxies.


ok thanks, I'll play around with these.
BTW. please excuse my poor english, but what's the meaning of "You're pretty
much toast" ?? My dictionary's explanation of "toast" doesn't seem to make
sense here...
Apr 15 '06 #4
> but what's the meaning of "You're
pretty much toast" ?? My dictionary's explanation of "toast" doesn't
seem to make sense here...


http://dictionary.reference.com/search?q=toast

Scroll down to the part that says:

Slang. One that is...
As far as I know it's a pretty well known expression throughout the English
speaking world (we use it here in Ireland).

-Tomás
Apr 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by (Pete Cresswell) | last post: by
4 posts views Thread by Peter K | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.