title: C++ call a non virtual function from virtual function in derived class
body: <p>In fact, similar questions were asked <a href="https://softwareengineering.stackexchange.com/questions/309124/how-does-the-base-class-non-virtual-function-get-called-when-derived-class-objec">here</a> and <a href="https://stackoverflow.com/questions/8796626/virtual-function-calling-a-non-virtual-function">there</a>, but the answers were not satisfied. The code example is</p>
  <pre><code>class  CBase
  {
  public:
      virtual void act1(){cout&lt;&lt;&quot;CBase::act1()! &quot;&lt;&lt;endl;  act2();}
      void act2()        {cout&lt;&lt;&quot;CBase::act2()! &quot;&lt;&lt;endl;  act3();}
      virtual void act3(){cout&lt;&lt;&quot;CBase::act3()! &quot;&lt;&lt;endl;  act4();}
      virtual void act4(){cout&lt;&lt;&quot;CBase::act4()! &quot;&lt;&lt;endl;  act5();}
      void act5()        {cout&lt;&lt;&quot;CBase::act5()! &quot;&lt;&lt;endl;    }
      virtual ~CBase(){}
  } ;
  
  class  CDerive : public  CBase
  {
  public:
      void act3(){cout&lt;&lt;&quot;CDerive::act3()! &quot;&lt;&lt;endl; act4();}
      void act4(){cout&lt;&lt;&quot;CDerive::act4()! &quot;&lt;&lt;endl; act5();}    
      void act5(){cout&lt;&lt;&quot;CDerive::act5()! &quot;&lt;&lt;endl;        }
      virtual ~CDerive(){}
  } ;
  int main()
  {
      CBase *p=new CDerive;
      p-&gt;act1();
      cout&lt;&lt;endl;
      p-&gt;act5();
      delete p;
      return 0;
  }
  </code></pre>
  <p>and the output is</p>
  <pre><code>CBase::act1()! 
  CBase::act2()! 
  CDerive::act3()! 
  CDerive::act4()! 
  CDerive::act5()! 
  
  CBase::act5()! 
  </code></pre>
  <p>With respect to <code>p-&gt;act1()</code></p>
  <ul>
  <li>since <code>act1()</code> is a virtual function and derived class doesn't implement it, the program will call <code>CBase::act1()</code>;</li>
  <li>then program will call <code>CBase::act2()</code>;</li>
  <li><code>act3()</code> is a vritual function, the program will call <code>CDervie::act3()</code>;</li>
  <li><code>act4()</code> is also a vritual function, the program will call <code>CDervie::act4()</code>;</li>
  <li>here comes the part that I don't understand, <code>act5()</code> is not a virtual function, and <code>p</code> is a pointer belonging to <code>CBase</code>, basically <code>p</code> can only access the function in <code>CBase</code>! But the output is <code>CDerive::act5()! </code></li>
  </ul>
  <p>In contrast, <code>p-&gt;act5()</code> will call <code>CBase::act5()</code> as I think.</p>
  <p>There seems to be contradictory between the principle - the base class pointer can only access the function defined in base class and virtual function, and the real output. The reason can't be explained from virtual table either, since <code>CDerive::act5()</code> is not even in the virtual table. So, my questions are</p>
  <ul>
  <li>what is the rationale behind those ?</li>
  <li>what did happen when <code>CBase *p=new CDerive</code> or <code>CDerive a; CBase *p=&amp;a</code>?</li>
  </ul>