title: Calling private member function in C++
body: <p>I came from Java where it's not allowed to decrease access modifiers in derived classes. For instnace, the following is not compile in Java:</p>
  
  <pre><code>public class A{
      public void foo(){ }
  }
  
  public class B extends A{
      @Override
      private void foo(){ }  //compile-error
  }
  </code></pre>
  
  <p>But, in C++ it's fine:</p>
  
  <pre><code>struct A {
      A(){ }
      virtual ~A(){ }
      A(A&amp;&amp;){ }
  public:
      virtual void bar(){ std::cout &lt;&lt; "A" &lt;&lt; std::endl; }
  };
  
  struct B : public A{
  private:
      virtual void bar(){ std::cout &lt;&lt; "B" &lt;&lt; std::endl; }
  };
  
  int main()
  {
      A *a = new B;
      a -&gt; bar(); //prints B
  }
  </code></pre>
  
  <p><a href="http://coliru.stacked-crooked.com/a/88628449141dc579" rel="nofollow"><strong>DEMO</strong></a></p>
  
  <p>Where might it be useful? Moreover, is it safe to do so? </p>