title: How to achieve a copy of an unknown Class which inherits a known class
body: <p>I need an array of varying class instances (here: B) which all share the same base class (here: A). <br/>
  The instances shall be stored as pointers, because they are referenced elsewhere (not displayed here).<br/>
  I tried it this way: <br /></p>
  
  <pre><code>class A {
  public:
    A() {}
  };
  
  class B : public A {
    int m_magicValue;
  public:
    B() : A() , m_magicValue(4711){}
    int getMagicValue() { return m_magicValue; }
  };
  
  class Manager {
    std::vector&lt;A*&gt; m_array;
  public:
    Manager() {}
    virtual ~Manager() {
        for( size_t i=0; i&lt;m_array.size(); i++ )
            delete m_array[i];
        m_array.clear();
  
    }
    void addA( const A&amp; a ) {
      // here A is copied - here the problem occurs
      m_array.push_back( new A(a) );
  //    createSomeReferences( m_array.back() );
    }
    A&amp; getA( size_t idx ) {
      return *m_array[idx];
    }
  };
  
  int main() {
    B b;
    Manager manager;
    manager.addA(b);
    B&amp; copiedB = (B&amp;) manager.getA(0);
    int magic = copiedB.getMagicValue(); // magic is some random stuff instead of 4711!
    return 0;   
  }
  </code></pre>
  
  <p>The Manager doesn't know anything about class B, it knows only A. <br/>
  The interesting thing happens in addA(): here it is attempted to new copy B and to store a pointer in the array.<br/>
  But as expected it doesn't work as expected ;-)
  copiedB.getMagicValue(); returns a random value.</p>
  
  <p>The reason is relatively clear: Instead of B's copy constructor A's copy constructor is used, so only the A parts are copied.</p>
  
  <p>So now my question:
  Is there a good way to achieve such a copy, which inherits the whole class hierarchy, even though only the base class is known?</p>