|
-
September 24th, 2002, 02:46 PM
#1
alternatives to "friend"
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?
-
September 24th, 2002, 03:17 PM
#2
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
-
September 25th, 2002, 04:12 AM
#3
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 25th, 2002, 06:38 AM
#4
Use of multiple inheritance indicates a need to redesign. Use
of 'friend'-ships is valid. How better to implement an iterator?
-
September 25th, 2002, 10:00 AM
#5
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
-
September 25th, 2002, 02:16 PM
#6
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
-
September 25th, 2002, 09:50 PM
#7
Re: Friend
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|