Accessing protected members in a derived class
<p>I ran into an error yesterday and, while it's easy to get around, I wanted to make sure that I'm understanding C++ right.</p>

<p>I have a base class with a protected member:</p>

<pre><code>class Base
{
  protected:
    int b;
  public:
    void DoSomething(const Base&amp; that)
    {
      b+=that.b;
    }
};
</code></pre>

<p>This compiles and works just fine.  Now I extend Base but still want to use b:</p>

<pre><code>class Derived : public Base
{
  protected:
    int d;
  public:
    void DoSomething(const Base&amp; that)
    {
      b+=that.b;
      d=0;
    }
};
</code></pre>

<p>Note that in this case <code>DoSomething</code> is still taking a reference to a <code>Base</code>, not <code>Derived</code>.  I would expect that I can still have access to <code>that.b</code> inside of <code>Derived</code>, but I get a <code>cannot access protected member</code> error (MSVC 8.0 - haven't tried gcc yet).</p>

<p>Obviously, adding a public getter on <code>b</code> solved the problem, but I was wondering why I couldn't have access directly to <code>b</code>.  I though that when you use public inheritance the protected variables are still visible to the derived class.</p>
