title: Can a friend class object access base class private members on a derived class object?
body: <p>I'm surprised that the code below compiles.</p>
  
  <p>It seems that a class befriended to the (publicly inherited) base class can access a member of the base class provided an instance of the derived class.</p>
  
  <p>If the inheritance is changed to <code>private</code> then compilation fails.</p>
  
  <p>In short, how is <code>d.b_var</code> valid within <code>F::func(D&amp; d)</code>?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class B{
      int b_var;
      friend class F;
  };
  
  class D: public B{
      int d_var;
  };
  
  class F{
      public:
          void func(D &amp;d){
              d.b_var = 5; 
          }
  };
  
  int main()
  {
      cout&lt;&lt;"fine";
  }
  </code></pre>