C++: Create object of certain type mapped to an enum
<p>Considering the following code example:</p>

<pre><code>class Base;
class A; class B; class C; //A, B and C are inherited from Base

enum TypeID
{
   TYPE_A = 0, TYPE_B, TYPE_C, TYPE_MAX;
}

class SomeContainer
{
    private Base* objects[ TYPE_MAX ];


    public void Add( TypeID what )
    {
        if( objects[ what ] ) return;

        switch( what )
        {
            case TYPE_A: objects[ TYPE_A ] = (Base*) new A;
            case TYPE_B: objects[ TYPE_B ] = (Base*) new B;
            case TYPE_C: objects[ TYPE_C ] = (Base*) new C;
            default:     printf( "Invalid type\n" ); return NULL;
        }
    }

    public A* GetA()
    {
        return (A*) objects[ TYPE_A ];
    }

    public B* GetB()
    {
        return (B*) objects[ TYPE_B ];
    }

    public C* GetC()
    {
        return (C*) objects[ TYPE_C ];
    }

}
</code></pre>

<p>I think it is better than words to describe what my real system currently does.</p>

<p>Now in reality I have even more classes derived from base, it's about 15.</p>

<ul>
<li><p>Do I have repetitive code for all of the classes derived from Base? As add a line for them in the switch statement, and an additional helper function to get them from the array?</p></li>
<li><p>Is there a way to automate this?</p></li>
</ul>
