title: polymorphism in c++, loss of type in vector of parent class
body: <p>I have a class A, which is parent to classes B and C.
  And a class X, which is a parent to Y and Z.</p>
  
  <pre><code>class A {};
  class B : public A {};
  class C : public A {};
  
  
  class X
  {
      void foo(A) { std:: cout &lt;&lt; "A"; }
  };
  
  class Y : public X
  {
      void foo(B) {std::cout &lt;&lt; "B"; }
  };
  
  class Z : public X
  {
      void foo(c) {std&lt;&lt;cout &lt;&lt;"C"; }
  };
  
  int main()
  {
      B b;
      C c;
  
      Y y;
      Z z;
  
      y.foo(b);//prints B // b is a B, and Y::foo takes a B, hence print B
      y.foo(c);//prints A // mismatch between types, fall back and print A
      z.foo(b);//prints A // mismatch between types, fall back and print A
      z.foo(c);//prints C // c is a C, and Y::foo takes a C, hence print C
  
      std::vector&lt;A&gt; v;
      v.push_back(b);
      v.push_back(c);
  
      //In this loop, it always prints A, but *this is what I want to change* 
      for (size_t i = 0; i &lt; v.size(); ++i)
      {
          z.foo(v.at(i));
          y.foo(v.at(i));
      }
  }
  </code></pre>
  
  <p>Is it possible to get the items to print the same result as the hard coded calls?
  Meaning that I will treat them as their original type, and not its parent type?
  or once I put them int a vector of A they will forever be of type A?</p>