Best way to access variables unique to derived class through container of baseclass
<p>So I have a vector of pointers to baseclass, which is used to hold all instances of derived classes.</p>

<p>Base class:</p>

<pre><code>#ifndef BASE_H
#define BASE_H

Class Base
{
  public:

    virtual void DoSomething();

};

#endif
</code></pre>

<p>A derived class:</p>

<pre><code>#ifndef DERIVED_H
#define DERIVED_H

#include "base.h"

Class Derived : public Base
{
  public:

    void DoSomething();

    float y;

};

#endif
</code></pre>

<p>With these things being stored inside:</p>

<pre><code>std::vector&lt;Base*&gt; theVec;
</code></pre>

<p>The question being, what is the best way to access the float variable "y" that exists only in Derived?</p>

<p>I could have a virtual function in Base that is specified in Derived to return a variable, where access looks like:</p>

<pre><code>theVec[0]-&gt;GetVar("y");
</code></pre>

<p>but when Derived is likely to have multiple variables of different types that are not in base this seems like it will end up being quite messy. Are there any ways to make access to a unique variable in Derived more generic? </p>

<p>Any suggestions would be greatly appreciated!</p>
