CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2002
    Posts
    71

    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?

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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


  4. #4
    Join Date
    May 2000
    Location
    Wi, USA
    Posts
    144
    Use of multiple inheritance indicates a need to redesign. Use
    of 'friend'-ships is valid. How better to implement an iterator?

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  6. #6
    Join Date
    Dec 1999
    Location
    Québec, Qc, Canada
    Posts
    26
    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

  7. #7
    Join Date
    Sep 2002
    Posts
    33

    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
  •  





Click Here to Expand Forum to Full Width

Featured