title: Public virtual method overridden as protected in inherited class
body: <p>I have a protected abstract virtual method <code>myMethod()</code> defined in class C. Class D inherits from C and defines <code>myMethod()</code>. Now class E also inherits from C and also defines <code>myMethod()</code>. So I have something like this:</p>
  
  <p>This looks like this</p>
  
  <pre><code>class C
  {
  protected:
      virtual void myMethod() = 0;
  }
  
  class D : public class C
  {
  protected:
      void myMethod() {/*Do something*/};
  
      void anotherMethod();
  }
  
  class E : public class C
  {
  protected:
      void myMethod() {/*Do something else*/};
  }
  </code></pre>
  
  <p>Now if in <code>D::anotherMethod()</code> I have a pointer to an object of class E then I cannot call <code>E::myMethod()</code>. Nothing wrong here: D and E have different hierarchies hence I cannot call <code>E::myMethod()</code> from D. i.e the below code does not compile which is expected:</p>
  
  <pre><code>void D::anotherMethod()
  {
      E* myE = new E();
  
      myE-&gt;myMethod();
  }
  </code></pre>
  
  <p>Now if I change the declaration of C and make <code>E::myMethod()</code> public (while keeping the overridden method in D and E <code>protected</code>), such as in the code below, it compiles:</p>
  
  <pre><code>class C
  {
  public:
      virtual void myMethod() = 0;
  }
  
  class D : public class C
  {
  protected:
      void myMethod() {/*Do something*/};
  
      void anotherMethod();
  }
  
  class E : public class C
  {
  protected:
      void myMethod() {/*Do something else*/};
  }
  </code></pre>
  
  <p>I only changed <code>public</code> to <code>protected</code> inside C, not in the inherited classes D and E.</p>
  
  <p>Does anyone know why it compiles and what is the logic behind it?</p>
  
  <p>Thanks!</p>
  
  <p>Antoine.</p>