friend'ly question

A

Amit Gupta

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
 
R

red floyd

Amit said:
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.
 
A

Amit Gupta

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.
 
P

Puppet_Sock

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
 
A

Amit Gupta

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.
 
T

terminator

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top