473,395 Members | 1,473 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,395 software developers and data experts.

friend'ly question

How do I do it (either by friend or by any other hack).

I have three class, I am just writing derivation below:

class A{}

class B{}

class C : public class B{}
class D: public class B ()

Now, class B would like to have access to A's private memebers (lets
say I make B a friend of A)

Now I also want C,D to have equal access to private members of class
A. I know, with friends definition, it is not possible, derived
classes dont inherit friendship of base class. Is there any other way
I can do this?

My flexibilities are:

I don't necessarily need to make C/D derived classes of B. I can do
something else too (what something else, I dont know but all I am
saying is, it is not REQUIRED to make C/D derived from B)

I don't want to make changes to A, once I write A, and write minimal
association of B to A (like B is friend of A), I want some way for C/
D, to access parts of B.
Now, the actual problem I have is, A is my design class
B is the first unit-test class I wrote for A, and I added B as friend
of A in design of A.
Few days later, I realized I need a new unit test for A, and therefore
I need a class C/D..but then I had to change A to make C/D friend of
A.

Another problem is, C/D are unit-tests that access multiplt design
classes. I could make all of design classes friend of single class B,
but then I dont want to change all those design classes to add C/D/E/
F.. as friend.
Thoughts, brain-storming is appreciated.

Thanks

Sep 14 '07 #1
5 2133
Amit Gupta wrote:
How do I do it (either by friend or by any other hack).

I have three class, I am just writing derivation below:

class A{}

class B{}

class C : public class B{}
class D: public class B ()

Now, class B would like to have access to A's private memebers (lets
say I make B a friend of A)

Now I also want C,D to have equal access to private members of class
A. I know, with friends definition, it is not possible, derived
classes dont inherit friendship of base class. Is there any other way
I can do this?

My flexibilities are:

I don't necessarily need to make C/D derived classes of B. I can do
something else too (what something else, I dont know but all I am
saying is, it is not REQUIRED to make C/D derived from B)

I don't want to make changes to A, once I write A, and write minimal
association of B to A (like B is friend of A), I want some way for C/
D, to access parts of B.
Now, the actual problem I have is, A is my design class
B is the first unit-test class I wrote for A, and I added B as friend
of A in design of A.
Few days later, I realized I need a new unit test for A, and therefore
I need a class C/D..but then I had to change A to make C/D friend of
A.

class A { friend class B; ... };
class B {
protected:
T access_A_member1();
T1 access_A_member_2(param1, param2);
// etc...
};

Then B's children can access A's private parts through the
access_A_{whatever} interface.
Sep 14 '07 #2
On Sep 14, 12:54 pm, red floyd <no.s...@here.dudewrote:
Amit Gupta wrote:
How do I do it (either by friend or by any other hack).
I have three class, I am just writing derivation below:
class A{}
class B{}
class C : public class B{}
class D: public class B ()
Now, class B would like to have access to A's private memebers (lets
say I make B a friend of A)
Now I also want C,D to have equal access to private members of class
A. I know, with friends definition, it is not possible, derived
classes dont inherit friendship of base class. Is there any other way
I can do this?
My flexibilities are:
I don't necessarily need to make C/D derived classes of B. I can do
something else too (what something else, I dont know but all I am
saying is, it is not REQUIRED to make C/D derived from B)
I don't want to make changes to A, once I write A, and write minimal
association of B to A (like B is friend of A), I want some way for C/
D, to access parts of B.
Now, the actual problem I have is, A is my design class
B is the first unit-test class I wrote for A, and I added B as friend
of A in design of A.
Few days later, I realized I need a new unit test for A, and therefore
I need a class C/D..but then I had to change A to make C/D friend of
A.

class A { friend class B; ... };
class B {
protected:
T access_A_member1();
T1 access_A_member_2(param1, param2);
// etc...

};

Then B's children can access A's private parts through the
access_A_{whatever} interface.
Thanks, But this implies, when I change A, I need to update B.

Furthermore, I have some private-function of A, so I need to wrap
around in B. While I will definitly use it If I dont have choice, I
will appreciate more comments on this topic, where the proposed
solution is least obtrusive.

Sep 14 '07 #3
On Sep 14, 5:02 pm, Amit Gupta <emaila...@gmail.comwrote:
On Sep 14, 12:54 pm, red floyd <no.s...@here.dudewrote:


Amit Gupta wrote:
How do I do it (either by friend or by any other hack).
I have three class, I am just writing derivation below:
class A{}
class B{}
class C : public class B{}
class D: public class B ()
Now, class B would like to have access to A's private memebers (lets
say I make B a friend of A)
Now I also want C,D to have equal access to private members of class
A. I know, with friends definition, it is not possible, derived
classes dont inherit friendship of base class. Is there any other way
I can do this?
My flexibilities are:
I don't necessarily need to make C/D derived classes of B. I can do
something else too (what something else, I dont know but all I am
saying is, it is not REQUIRED to make C/D derived from B)
I don't want to make changes to A, once I write A, and write minimal
association of B to A (like B is friend of A), I want some way for C/
D, to access parts of B.
Now, the actual problem I have is, A is my design class
B is the first unit-test class I wrote for A, and I added B as friend
of A in design of A.
Few days later, I realized I need a new unit test for A, and therefore
I need a class C/D..but then I had to change A to make C/D friend of
A.
class A { friend class B; ... };
class B {
protected:
T access_A_member1();
T1 access_A_member_2(param1, param2);
// etc...
};
Then B's children can access A's private parts through the
access_A_{whatever} interface.

Thanks, But this implies, when I change A, I need to update B.
Yes, you do need to update B when you change A. But that's
not because B has provided these access functions. It's
because B is a friend of A.

If you give your friend a key to your apartment, then change
the locks on your door, you have to update your friend's key
as well as your own. This is true whether or not your friend
lets other people into your apartment.
Socks

Sep 14 '07 #4
Yes, you do need to update B when you change A. But that's
not because B has provided these access functions. It's
because B is a friend of A.

If you give your friend a key to your apartment, then change
the locks on your door, you have to update your friend's key
as well as your own. This is true whether or not your friend
lets other people into your apartment.
Socks
Yes Socks- But I am not changing locks to the apartment,
I am just changing the furniture inside.

Regardless, I am not inclined to go into debate of *WHY* is it this
way.
This question is for C++ hackers, who can find a way out.

Sep 15 '07 #5
On Sep 14, 10:25 pm, Amit Gupta <emaila...@gmail.comwrote:
How do I do it (either by friend or by any other hack).

I have three class, I am just writing derivation below:

class A{}

class B{}

class C : public class B{}
class D: public class B ()

Now, class B would like to have access to A's private memebers (lets
say I make B a friend of A)

Now I also want C,D to have equal access to private members of class
A. I know, with friends definition, it is not possible, derived
classes dont inherit friendship of base class. Is there any other way
I can do this?

My flexibilities are:

I don't necessarily need to make C/D derived classes of B. I can do
something else too (what something else, I dont know but all I am
saying is, it is not REQUIRED to make C/D derived from B)

I don't want to make changes to A, once I write A, and write minimal
association of B to A (like B is friend of A), I want some way for C/
D, to access parts of B.

Now, the actual problem I have is, A is my design class
B is the first unit-test class I wrote for A, and I added B as friend
of A in design of A.
Few days later, I realized I need a new unit test for A, and therefore
I need a class C/D..but then I had to change A to make C/D friend of
A.

Another problem is, C/D are unit-tests that access multiplt design
classes. I could make all of design classes friend of single class B,
but then I dont want to change all those design classes to add C/D/E/
F.. as friend.
Does multiple inheritance do?
like this:

class A{
freind class B;
};

class B{
freind class A;
};

struct mix: B,A{};
//now derive everything from mix:

struct C:mix{};
struct D:mix{};

regards,
FM.

Sep 15 '07 #6

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

Similar topics

1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
11
by: Brian | last post by:
Hi! I am new to VB.net but know enough to kind of dive in. I want to make a text adventure but I am having problems in how I want to store the data for inventory items, characters and such....
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
2
by: Marco Spatz | last post by:
Hi, we moved our build platform to the "next" generation ;) (VS 2005 and gcc 4.0) and our code has build (with some minor changes) so far. But now I met a strange problem with the friend keyword...
5
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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...
0
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...
0
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...

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.