title: Why can I access a derived private member function via a base class pointer to a derived object?
body: <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  class base
  {
  public:
      virtual void add() {
          cout &lt;&lt; "hi";
      }
  };
  
  class derived : public base
  {
  private:
      void add() {
          cout &lt;&lt; "bye";
      }
  };
  
  int main()
  {
      base *ptr;
      ptr = new derived;
      ptr-&gt;add();
      return 0;
  }
  </code></pre>
  
  <p>Output is <code>bye</code></p>
  
  <p>I dont have a problem with how this is implemented. I understand you use vtables and the vtable of derived contains the address of the new add() function. But add() is private shouldn't compiler generate an error when I try to access it outside the class? Somehow it doesn't seem right.</p>