473,383 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 1496
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
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
3
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...
7
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...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
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
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...
4
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...
6
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 {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.