title: Get a reference of a derived class object through a base class pointer
body: <p>I have a vector of base class pointers to store different derived classes. I need to access methods/objects that may be present on a derived class, but not on the base class.</p>
  
  <p>I've tried using a function in the form of something like <code>base* getObject(int idx);</code> and then using <code>dynamic_cast</code>, and <code>static_cast</code> from <code>Base*</code> into <code>Derived&amp;</code>, but that doesn't seem to work, as compilation failed.</p>
  
  <p>My base class looks something like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>        class Base {
            int a, b;
          public:
            virtual void f1(int) = 0;
            virtual void f2(int, int) = 0;
          };
  </code></pre>
  
  <p>And my derived classes look something like this:</p>
  
  <pre><code>        class Derived : public Base {
            std::vector &lt;int&gt; myVector;
          public:
            void f1(int ai) override {}
            void f2(int ai, int bi) override {}
            int getFromVector(int idx) { return myVector[idx]; }
          }
  </code></pre>
  
  <p>The vector that should contain the derived classes looks like this:</p>
  
  <pre><code>        std::vector &lt;Base*&gt; derivedObjects;
  </code></pre>
  
  <p>What I would like to do, is to get something from the <code>derivedObjects</code> vector, and access, say, <code>getFromVector()</code>.</p>