Click to See Complete Forum and Search --> : alternatives to "friend"


C++ Newbie
September 24th, 2002, 02:46 PM
One poster on this board mentioned that use of friend indicates a poor design. Is this true? If so, what are better ways to accomplish the same result?

jfaust
September 24th, 2002, 03:17 PM
Redesign. If there's a flaw in your design, it's not a simple matter of doing it differently, but in taking it apart, rethinking it, and designing it. If it's designed well, this issue will not come up.

There are valid uses of 'friend', but most are not.

Jeff

Graham
September 25th, 2002, 04:12 AM
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 friends may be preferable to one that does.

Dave Sell
September 25th, 2002, 06:38 AM
Use of multiple inheritance indicates a need to redesign. Use
of 'friend'-ships is valid. How better to implement an iterator?

jfaust
September 25th, 2002, 10:00 AM
Use of multiple inheritance indicates a need to redesign.


Not necessarily. There are good ways to use multiple inheritance, although it's a language feature that is easily abused.

Jeff

Martin Boucher
September 25th, 2002, 02:16 PM
It's like anything else. There are tools or concepts that exist and there are situations in which it is appropriate to use those tools. Saying "using friendship is bad design" is the same as saying "using an hammer is bad". You've got to put the tool in a context to evaluate if it's bad or not. I prefer using friends than putting everything public in my interface. It's a way of selecting v.i.p. member of your class. If you know about Design Patterns, you can see that the State pattern is a good candidate to add friendship in the design.

Martin

TruBic
September 25th, 2002, 09:50 PM
I think there's nothing 100% bad and nothing 100% good. What bad and good is how we use it. Friend class can save us a lot of lengthy code and time. The idea is any class that we intent to use internally (i.e for implementation of some other higher level problem) we can make it friend. For classes that we want to published for public use then friend should not be allowed.