title: Slicing in C++ where I am wrong?
body: <p>I read about the slicing problem in C++ and I tried some examples (I am from Java background). Unfortunatelly, I do not understand some of the behaviour. Currently, I am stucked in this example (alternated example from Efficent C++ Third Edition). Can anybody help me to understand it?</p>
  
  <p>My simple class of a parent:</p>
  
  <pre><code>class Parent
  {
  public:
  
      Parent(int type) { _type = type; }
  
      virtual std::string getName() { return "Parent"; }
  
      int getType() { return _type; }
  
  private:
  
      int _type;
  };
  </code></pre>
  
  <p>My simple class of a Child:</p>
  
  <pre><code>class Child : public Parent
  {
  
  public:
  
      Child(void) : Parent(2) {};
  
      virtual std::string getName() { return "Child"; }
      std::string extraString() { return "Child extra string"; }
  };
  </code></pre>
  
  <p>The main:</p>
  
  <pre><code>void printNames(Parent p)
  {
      std::cout &lt;&lt; "Name: " &lt;&lt; p.getName() &lt;&lt; std::endl;
  
      if (p.getType() == 2)
      {
          Child &amp; c = static_cast&lt;Child&amp;&gt;(p);
          std::cout &lt;&lt; "Extra: " &lt;&lt; c.extraString() &lt;&lt; std::endl;
          std::cout &lt;&lt; "Name after cast: " &lt;&lt; c.getName() &lt;&lt; std::endl;
      }
  }
  
  int main()
  {
      Parent p(1);
      Child c;
  
      printNames(p);
      printNames(c);
  }
  </code></pre>
  
  <p>Afte the execution I get:</p>
  
  <p><strong>Name: Parent</strong></p>
  
  <p><strong>Name: Parent</strong></p>
  
  <p><strong>Extra: Child extra string</strong></p>
  
  <p><strong>Name after cast: Parent</strong></p>
  
  <p>I understand the first two lines, because it is the cause of the "slicing". But I do not understand why can I cast the child into parent via the static cast. In the book, there is written that after the slicing, all the specialized information will be sliced off. So I suppose, I cannot cast <strong>p</strong> into <strong>c</strong>, because I do not have infomation (in the beginning of the function printNames a new Parent object is created without the additional information).</p>
  
  <p>Additionaly, why I am getting the "Name after cast: Parent" if the cast was successful instead of "Name after cast: Child"? </p>