title: member variable inheritance
body: <p>I have this code, note that two lines have been commented out</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class foo {
  public:
      foo();
      int i;
  };
  
  class bar: foo {
  public:
      bar();
      //int i;
  };
  
  foo::foo()
  {
      i = 2;
  }
  
  bar::bar()
  {
      i = 4;
  }
  
  int main()
  {
      bar *b = new(bar);
      //std::cout &lt;&lt; "bi = " &lt;&lt; b-&gt;i &lt;&lt; std::endl; /*line 28*/
      foo *f = (foo*) b;
      std::cout &lt;&lt; "fi = " &lt;&lt; f-&gt;i &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>With the two lines commented out, the code compiles and the output is</p>
  
  <pre><code>fi = 4
  </code></pre>
  
  <p>With the two lines uncommented, the code compiles and the output is</p>
  
  <pre><code>bi = 4
  
  fi = 2
  </code></pre>
  
  <p>With only the declaration of i within class bar commented out the compilation fails</p>
  
  <pre><code>var.cc: In function ‘int main()’:
  
  var.cc:6:7: error: ‘int foo::i’ is inaccessible
  
  var.cc:28:30: error: within this context
  </code></pre>
  
  <p>I understand the first two cases but I do not understand this compilation error. Why is</p>
  
  <p>the variable "i" accessible from within the bar constructor but not from a bar pointer?</p>