CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

View Poll Results: Does "friend" violate encapsulation?

Voters
18. You may not vote on this poll
  • Yes, it does.

    7 38.89%
  • No, it doesn't.

    11 61.11%
Page 1 of 6 1234 ... LastLast
Results 1 to 15 of 77
  1. #1
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Does "friend" violate encapsulation?

    Many people say "yes, it does", Bjarne Stroustrup says "no, it doesn't".

    What is your opinion?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Does "friend" violate encapsulation?

    I think that it can be argued either way. For example, you can argue on the side of Stroustrup that a class should have a minimal but complete interface... because both member functions and friends "violate encapsulation".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Does "friend" violate encapsulation?

    I think it does. It goes against the purpose of using functions to restrict access to data, for one. It does have its place though.. I use it whenever it is justified (which is occasionally).
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by Mybowlcut
    It goes against the purpose of using functions to restrict access to data, for one.
    How does it go against the purpose of using functions to restrict access to data? Access to the internals of the class is restricted to the member functions, friend functions, and member functions of friend classes.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Does "friend" violate encapsulation?

    I've always seen friend functions as a part of the class's interface.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Does "friend" violate encapsulation?

    I meant in the sense that allowing access to data directly through the use of friend instead of through (non-friend) member functions restricts the ability of the data to change without affecting other code. It's a small issue - and that's why I continue to use friend, as I don't see it as that much of a problem - but it's still an issue.
    Last edited by Mybowlcut; June 10th, 2009 at 03:35 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by Mybowlcut
    I meant in the sense that there is less ability to change the data without affecting other code.
    Ah yes, the same point that I was making that one could consider that member functions "violate encapsulation".

    EDIT:
    Quote Originally Posted by Mybowlcut
    I meant in the sense that allowing access to data directly through the use of friend instead of through (non-friend) member functions restricts the ability of the data to change.
    Then you are wrong. What makes it easier to allow the internals of the class to change is the use of non-member non-friend functions since they do not directly access the internals of the class. Member functions also access (or at least can access) the internals of the class, so changing the data members of a class can cause a member function to stop working.
    Last edited by laserlight; June 10th, 2009 at 03:39 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by laserlight View Post
    Ah yes, the same point that I was making that one could consider that member functions "violate encapsulation".

    EDIT:

    Then you are wrong. What makes it easier to allow the internals of the class to change is the use of non-member non-friend functions since they do not directly access the internals of the class. Member functions also access (or at least can access) the internals of the class, so changing the data members of a class can cause a member function to stop working.
    Yes, that does it make it easier, but using friend will still open up places where a change in the class' data can result in other code being affected. Member functions are slightly better than friend I think, because there is more fine-grained control, whereas with friend you have access to everything.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by Mybowlcut
    Yes, that does it make it easier, but using friend will still open up places where a change in the class' data can result in other code being affected.
    Of course, I never disputed that. You might want to read How Non-Member Functions Improve Encapsulation.

    Quote Originally Posted by Mybowlcut
    Member functions are slightly better than friend I think, because there is more fine-grained control, whereas with friend you have access to everything.
    As far as I can tell, member functions also have "access to everything". One difference is that it is possible to designate as a friend a function or class that is not under your control, whereas it is not as likely that a member function of your class is not under your control.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by laserlight View Post
    Of course, I never disputed that. You might want to read How Non-Member Functions Improve Encapsulation.
    It's cool, I have Effective C++ and have read the item on the topic.
    Quote Originally Posted by laserlight View Post
    As far as I can tell, member functions also have "access to everything".
    Yeah, they do. But what I was saying was that member functions allow different levels of access, which is closer to offering encapsulation than the use of friend.
    Quote Originally Posted by laserlight View Post
    One difference is that it is possible to designate as a friend a function or class that is not under your control, whereas it is not as likely that a member function of your class is not under your control.
    Not really sure what you mean by this.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by JohnW@Wessex View Post
    I've always seen friend functions as a part of the class's interface.
    Actually, I should probably qualify that.

    There are certain functions that can be seen to be part of a class's interface, but are better defined, for various design reasons, as non-member functions. Some of these may need access to the implementation details of the class and therefore must be 'friends'.

    Example.
    A numeric class may define overloaded operators to give the same 'look & feel' as built-in types. They could be considered to be part of the interface for the class.
    The operators == != < + - are usually defined as non-member functions and may need to be declared as friends.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Does "friend" violate encapsulation?

    I have a quite different view on friend functions and friend classes.

    Friend functions are clearly, how Stroustrup describes it, part of the interface. As each friend function needs to be explicitly declared by the class designer, the control of the interface is still with the class designer.

    Friend classes are imho a sign of flawed design. Assuming class A declares class B as friend, then the interface of class A includes a big cloud (class B) where other people than the designer of class A (namely the designer of class B) can freely add/remove/change functionality. So class A does not have a clear interface any more, only the combination of class A and class B (and whatever other friends they have) have a common interface.

    Note that all design pattern which are usually achieved by friend classes can also, with a little more writing, be achieved with friend functions. With the advantage, that each class definition defines the complete interface to the class.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  13. #13
    Join Date
    Sep 2008
    Posts
    113

    Re: Does "friend" violate encapsulation?

    I'm still new to C++ and I've been up all night, so I'll try to get my thoughts across as clearly as possible.

    If you're the creator of both A and B, then friend classes can allow you to more easily and less awkwardly create a complete interface between the two. Class A could have its own interface, and then Class B could build upon that, for example.

    It depends on the relationship between the two classes. In the example I was given when I learned about friend classes (a TV class and a remote class) I couldn't think of anything that made more sense than considering them "friends."

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Does "friend" violate encapsulation?

    I've used friend classes occasionally where a closely related set of classes must interact, but I didn't want the interface for this interaction to be available to the wider application.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  15. #15
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Does "friend" violate encapsulation?

    Quote Originally Posted by JohnW@Wessex
    I've used friend classes occasionally where a closely related set of classes must interact, but I didn't want the interface for this interaction to be available to the wider application.
    That's exactly the reason I use friends - great way to describe it.

    Anyone else think that there are not enough code examples in this thread?
    Quote Originally Posted by Raislin
    If you're the creator of both A and B, then friend classes can allow you to more easily and less awkwardly create a complete interface between the two. Class A could have its own interface, and then Class B could build upon that, for example.

    It depends on the relationship between the two classes. In the example I was given when I learned about friend classes (a TV class and a remote class) I couldn't think of anything that made more sense than considering them "friends."
    For instance, could you code up a quick example of the TV and remote as "friends" to show why the relationship is necessary?

    Quote Originally Posted by treuss
    Note that all design pattern which are usually achieved by friend classes can also, with a little more writing, be achieved with friend functions. With the advantage, that each class definition defines the complete interface to the class.
    Could you show us what you mean by this?

    For people like me who struggle visualising concepts, code can really help. I'm sure other people here are interested in knowing as well, even if they don't speak out.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Page 1 of 6 1234 ... LastLast

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