title: Difference in usage of pointer downcast & upcast?
body: <p>I am wondering what really happen in pointer casting when we are using downcast and upcast.
  I have 2 question. First 2 of them is the comment. Q3 is in the end.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  class A{
    public:
      virtual void  f()
      {
        cout&lt;&lt;"A"&lt;&lt;endl;
      }
  };
  class B: public A{
    public:
       virtual void   f()
      {
        cout&lt;&lt;"B"&lt;&lt;endl;
      }
  };
  int main()
  {
    A* pa =new A();
    B* pb =new B();
    A* paUpcast= new B();
    /*
      Q1:
      Is the line above equivalent to the following?
      A* paUpcast = (A*) B;
    */
    B* pbDowncast=(B*)paUpcast;
    /*
      Q2:Why we cannot use the following 2 code;
      B* pbDowncast=new A();
      B* pbDowncast = (B*) pa;
    */
    pa-&gt;f();
    pb-&gt;f();
    paUpcast-&gt;f();
    pbDowncast-&gt;f();
  
  
    return 1;
  }
  </code></pre>
  
  <p>Q3: I am trying to summary a rule to infer what will happene if we use the virtual function and pointer cast together, but I just cannot figure it out. </p>
  
  <p>Originally, I think virtual function will lead us to where the pointer really pointed out.
  Therefore, when we type </p>
  
  <pre><code>A* paUpcast= new B();
  paUpcast-&gt;f();
  </code></pre>
  
  <p>the second line will display "B" if A.f() is virtual function, because paUpcast is actually point to B object</p>
  
  <p>However, when we type </p>
  
  <pre><code>B* pbDowncast=(B*)pa;
  pbDowncast-&gt;f();
  </code></pre>
  
  <p>and it will display "A" instead of "B", which make the contradiction happen. </p>
  
  <p>Can anyone explain or show me some tip? Thanks alot</p>