title: clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)
body: <p>Why does C++ have <code>public</code> members that anyone can call and <code>friend</code> declarations that expose <em>all</em> <code>private</code> members to <em>given</em> foreign classes or methods but offer no syntax to expose particular members to given callers?</p>
  
  <p>I want to express interfaces with some routines to be invoked only by known callers without having to give those callers complete access to all privates, which feels like a reasonable thing to want.  The best I could come up with myself (below) and suggestions by others so far revolve around idioms/pattern of varying indirectness, where I really just want a way to have <em>single</em>, simple class definitions that explicitly indicate what callers (more granularly than <em>me</em>, <em>my children</em>, or <em>absolutely anybody</em>) can access which members.  What is the best way to express the concept below?</p>
  
  <pre><code>// Can I grant Y::usesX(...) selective X::restricted(...) access more cleanly?
  void Y::usesX(int n, X *x, int m) {
    X::AttorneyY::restricted(*x, n);
  }
  
  struct X {
    class AttorneyY;          // Proxies restricted state to part or all of Y.
  private:
    void restricted(int);     // Something preferably selectively available.
    friend class AttorneyY;   // Give trusted member class private access.
    int personal_;            // Truly private state ...
  };
  
  // Single abstract permission.  Can add more friends or forwards.
  class X::AttorneyY {
    friend void Y::usesX(int, X *, int);
    inline static void restricted(X &amp;x, int n) { x.restricted(n); }
  };
  </code></pre>
  
  <p>I'm nowhere near being a software organization guru, but it feels like interface simplicity and the principle of least privilege are directly at odds in this aspect of the language.  A clearer example for my desire might be a <code>Person</code> class with declared methods like <code>takePill(Medicine *)</code> <code>tellTheTruth()</code> and <code>forfeitDollars(unsigned int)</code> that only <code>Physician</code>, <code>Judge</code>, or <code>TaxMan</code> instances/member methods, respectively, should even consider invoking.  Needing one-time proxy or interface classes for each major interface aspect sits ill with me, but please speak up if you know I'm missing something.</p>
  
  <p><strong>Answer accepted from <a href="https://stackoverflow.com/users/23934/drew-hall">Drew Hall</a>: <a href="http://www.drdobbs.com/184402053" rel="noreferrer">Dr Dobbs - Friendship and the Attorney-Client Idiom</a></strong></p>
  
  <p>The code above originally called the wrapper class 'Proxy' instead of 'Attorney' and used pointers instead of references but was otherwise equivalent to what Drew found, which I then deemed the best generally known solution.  (Not to pat myself on the back too hard...)  I also changed the signature of 'restricted' to demonstrate parameter forwarding.  The overall cost of this idiom is one class and one friend declaration per permission set, one friend declaration per set approved caller, and one forwarding wrapper per exposed method per permission set.  Most of the better discussion below revolves around the forwarding call boilerplate that a very similar 'Key' idiom avoids at the expense of less direct protection.</p>