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

"friendship" question

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

P: n/a

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

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

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