473,813 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 1729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
4608
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document, and at the end there is even pure mindstorming text left in, but already I think it can be very useful. <url: http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha.doc.pdf>.
6
2127
by: (Pete Cresswell) | last post by:
I'm makeing a little DB to help manage high school class reunions. One feature I'm trying to implement is "Friends". e.g. Fred Smith is a friend of Joe Jones and Bill Anderson. We record that so that when Fred moves and our mailing bounces, we can call Joe and/or Bill in hopes of getting Fred's new address. I started implementing this as just tblFriend linked to tblPerson on PersonID plus another link to tblPerson on FriendID.
43
2854
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference (calling tree) browsing that is supported in C++. I don't know how some could write a large scale object-oriented application w/o those. If you have overcome these limitations in C#, please share your thoughts and ideas. Thanks!
14
2843
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
4
1877
by: Peter K | last post by:
Hi if I am developing a "class library" which contains some classes which should be exposed to the outside world, and other classes which I really would rather have "private" to my library (helper/utility classes), how do I achieve this? Obviously public classes can just be declared "public". But how do I make classes available only to the other classes which are a part of my library?
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10417
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10139
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7678
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.