templates in factory pattern
<p>When I was explaining a factory pattern which takes in a enum to its createobject static method, a colleague asked how can we make it such that we dont have to add a switch case every time we have a new derived class defined. Something on the below lines. I know it wont compile(just to put the idea better)</p>

<pre><code>template &lt;class T&gt;
class theCreator {
  public:
    static T* createInstance(int a_ichoice) {
      return new T();
    }
};

class theBase {
  public:
    virtual void SayyourName() = 0;
};

class theDerived1 : theBase {
  public:
    void SayyourName() {
      cout &lt;&lt; typeid(this).name();
    }
};

class theDerived2 : theBase {
  public:
    void SayyourName() {
      cout &lt;&lt; typeid(this).name();
    }
};

int main() {
  theBase * p = theCreator::createInstance(1);
  p-&gt;SayyourName();
}
</code></pre>
