473,668 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friendship with dependencies

Is there a way to do anything like the following:

class A
{
friend void B::foo();

...
};

class B
{
void foo();
...
A my_a;
};

This is very much simplified, but basically B depends upon A so that A
must be defined first, yet I want to make one member function of B a
friend of A.

Thanks,
Mark
Oct 12 '06 #1
2 1514
Mark P wrote:

Is there a way to do anything like the following:

class A
{
friend void B::foo();
...
};

class B
{
void foo();
...
A my_a;
};

This is very much simplified, but basically B depends upon A so that A
must be defined first, yet I want to make one member function of B a
friend of A.
Two ways come to mind, neither of which is wholly satisfactory. First,
make B, rather than B::foo(), a friend of A. This makes the friendship
broader than required, but does not require you to define B before
defining A.

Second, you could change B::my_a to either a pointer or a reference to
A, instead of an actual A. That would permit you to forward-declare A,
then define B, then define A. A could in that definition grant
friendship to B::foo, as you require.

Best regards,

Tom

Oct 12 '06 #2

Mark P wrote:
Is there a way to do anything like the following:

class A
{
friend void B::foo();

...
};

class B
{
void foo();
...
A my_a;
};

This is very much simplified, but basically B depends upon A so that A
must be defined first, yet I want to make one member function of B a
friend of A.

Thanks,
Mark
What for? B::foo() has access to my_a regardless of their respective
access specifiers with respect to B. In the case where class A has some
private part you want B::foo() to have access to, it makes more sense
to modify A's interface.
Friendifying B or B::foo() is more likely to create new issues rather
than providing a maintainable fix.
For example, friends are not inherited.

Also, the member must be constructed first but that is entirely under
the control of the Container's creation process (unless, of course, you
forfeit the right to do so).

example:

class A
{
int a;
public:
A() : a(0) { } // def ctor, guarantees a valid int
A(int n) : a(n) { }
void setA(int n) { a = n; }
};

class B
{
A a;
public:
B() : a() { } // or whatever...B() : a(-1) { }
B(int n) : a(n) { }
void foo() { a.setA(99); }
};

Its now impossible to generate an instance of A or B with uninitialized
members.

Oct 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7525
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
3
1410
by: Dave | last post by:
Hello all, I need an idiom to do the following: I have a set of non-public member functions in my class. I would like to grant access to *some* of them to another class. I can't just declare the other class a friend because that would grant access to *all* member functions. Is there any way I can accomplish this?
7
1710
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip Policy solves the problem. There are different proposals how to solve the problem: The first idea is to provide the class with a macro --------------------------------------------------------------------- #define FRIENDS_OF_FOO class A; class B;...
4
1959
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
2074
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
4025
by: tom.s.smith | last post by:
I know that friendship isn't inherited in C++, and have two questions. (1) Why? Is it a technical problem, or just by design? If the latter, isn't this a little prescriptive, and shouldn't there be a way of explicitly inheriting friendship? (eg, class B : friendly public A) (2) Is there a way I can simulate inheritance of friendship? I'm working on an output library with a design based on that of iostreams: there is a class called an...
4
1778
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes aware of the private details of a class. This means that it is more difficult to change those private details, since there are other things around that depend on them. It doesn't necessarily indicate poor design, but a design that doesn't use...
6
1777
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8653
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...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2786
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
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
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.