3

Use of friend function seems like a little hack to me. Do friend functions violate the concept of encapsulation?

What are the alternatives to friend function? Will using a simple helper class/function along with setter & getter member functions help increase encapsulation in comparison with just using friend?

Geezer
  • 5,600
  • 18
  • 31
ark1974
  • 615
  • 5
  • 16
  • 1
    Well, a simple class with only a getter and a setter is... no different than having these member public. You're only lying to yourself. A friend function is actually more helpful to encapsulate, since you target directly who can access your members. – Guillaume Racicot Sep 09 '18 at 03:14
  • @GuillaumeRacicot: But will sharing of such `secret keys` with others create confusion in the code ( esply big projects) later - const-ness of member functions etc – ark1974 Sep 09 '18 at 03:19
  • 2
    [When should you use 'friend' in C++?](https://stackoverflow.com/q/17434/62576) (found with a quick search) might help. – Ken White Sep 09 '18 at 03:32
  • 3
    Scott Meyers has written a number of articles about member functions, non-member friends, and non-member non-friends - and how they can affect encapsulation. You'll be able to find a fair number with google. Some of the details of his thinking have changed over time. Bear in mind that encapsulation is not really a desirable property in its own right - it is one way of controlling and localising effects of change and therefore contributes to properties like flexibility (ability to control change) and robustness (not introducing unintended flaws when making change). – Peter Sep 09 '18 at 04:19
  • 1
    This question is not opinion-based if you measure the degree of encapsulation as the amount of code which the compiler allows access to another piece of code. – Christian Hackl Sep 11 '18 at 05:17

2 Answers2

4

Do friend functions violate the concept of encapsulation?

In fact, it doesn't. First of, notice it doesn't violate encapsulation more than public members do. Are public members a violation of the concept of encapsulation? Obviously not. More so, in many situations friend increases encapsulation rather then violates it, as it allows you to maintain the private stuff as private, instead of having to expose data system-wide using public members, which makes the entire concept of it being private hidden from the naked eye. This is the opposite of delivering intent in your code, and should raise a red flag when noticed. friend allows you to mitigate this issue, as it gives you control over exactly who can access these members that are being kept private for everyone to know they are. The C++ FAQ raises one example in which you can use friend to solve a design issue such that you exercise this control over the access to a point where you haven't increased at all the amount of code that has access to the private parts.

Shouldn't a simple helper class/function along with setter & getter member functions can rescue this design flaw ?

Well, using a helper class could definitely answer some of the same needs that friend do, but many times are unjustifiably more cumbersome than just using friend. About function or getter/setter, that just sounds to me like using public members to directly expose private ones, the disadvantages of which are described above.

... or shouldn't it be treated as a poor design practice ?

As an end note, try avoiding subjective questions in here, like whether something should or shouldn't be treated as X. Let's stick to facts! : )

Geezer
  • 5,600
  • 18
  • 31
1

Does friend function violates concept of encapsulation

No, it enhances encapsulation because the alternative would be to make the member public.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62