473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2147
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_member 1();
T1 access_A_member _2(param1, param2);
// etc...
};

Then B's children can access A's private parts through the
access_A_{whate ver} interface.
Sep 14 '07 #2
On Sep 14, 12:54 pm, red floyd <no.s...@here.d udewrote:
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_member 1();
T1 access_A_member _2(param1, param2);
// etc...

};

Then B's children can access A's private parts through the
access_A_{whate ver} 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...@gmai l.comwrote:
On Sep 14, 12:54 pm, red floyd <no.s...@here.d udewrote:


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_member 1();
T1 access_A_member _2(param1, param2);
// etc...
};
Then B's children can access A's private parts through the
access_A_{whate ver} 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...@gmai l.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
4428
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 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix...
6
3313
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 my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A...
3
2068
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. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
0
1686
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > > ============================================================== > Your code refuses to compile. In function 'foo<a>' 't' is a pointer to > const. Friendship has nothing to do with it. When I...
15
6566
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local...
11
1205
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. Right now, I have three rooms but a lot of if and then statements on how to tell if they should appear in your (listboxes) inventory or items in room...
2
2158
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 Debug_Level ddl;
2
1631
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 (I know I should be careful using this ;) ). I made a reproducer for it: class ClassA
5
1880
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
0
7499
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...
0
7689
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. ...
0
7943
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...
1
7456
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...
0
7786
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...
1
5359
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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
0
743
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...

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.