title: subtle C++ inheritance error with protected fields
body: <p>Below is a subtle example of accessing an instance's protected field x.
  B is a subclass of A so any variable of type B is also of type A.
  Why can B::foo() access b's x field, but not a's x field? </p>
  
  <pre><code>class A {
  protected:
    int x;
  };
  
  class B : public A {
  protected:
    A *a;
    B *b;
  public:
    void foo() {
      int u = x;     // OK : accessing inherited protected field x
      int v = b-&gt;x;  // OK : accessing b's protected field x
      int w = a-&gt;x;  // ERROR : accessing a's protected field x
    }
  };
  </code></pre>
  
  <p>Here is the error I get with g++</p>
  
  <pre><code>$ g++ -c A.cpp
  A.cpp: In member function ‘void B::foo()’:
  A.cpp:3: error: ‘int A::x’ is protected
  A.cpp:14: error: within this context
  </code></pre>