How to provide a single function to get data of any derived object through base class pointer?
<p>i have code like below.
Base is the base class and D1, D2, D3 are derived classes.
D1, D2 and D3 class objects can hold int, float and double values respectively.
I have a vector of base class pointers. Each one of them can point to any of the derived class objects.
Through Base class pointer i should be able to get the data present in any of the derived class.</p>

<pre><code>enum Type
{
   INT,
   FLOAT,
   DOUBLE
};
struct Data
{
   Type type;
   union
   {
       int iVal;
       float fVal;
       double dVal;
   };
};
class Base
{
public:
   virtual Data getData() = 0;
};
class D1: public Base
{
   int i;
public:
   virtual Data getData()
   {
       Data data;
       data.type = INT;
       data.iVal = i;
       return data;
   }
};

class D2: public Base
{
     float f;
public:
    virtual Data getData()
    {
       Data data;
       data.type = FLOAT;
       data.fVal = f;
       return data;
    }
};

class D3: public Base
{
     double d;
public:
    virtual Data getData()
    {
       Data data;
       data.type = DOUBLE;
       data.dVal = d;
       return data;
    }
};
</code></pre>

<p>getData() returns Data object. </p>

<p>Using the type in Data object, i need to extract corresponding value in union.</p>

<p>I am unable to avoid type checking even though i am using virtual function mechanism.</p>

<p>Is there any better way to return data through single interface function ?</p>

<p><strong>EDIT</strong>: In my project scenario, at runtime it can generate few D1 objects, few D2 objects and so on. I need to pass all these objects to some other module.
So i used <code>vector&lt;Base *&gt;</code> to pass on all the objects to the next module.
Is there any better way to pass on all the objects ?</p>

<p><strong>EDIT</strong>: Any Non-Boost solution exists ?</p>
