title: Template based member initialisation, where the template is a derivative of an abstract class
body: <p>I'm working on the implementation of a genetic algorithm that is supposed to work on an abstract genome type. </p>
  
  <p>My setup is something like this:</p>
  
  <pre><code>class AbstractGenome
  {
    virtual void init() = 0;
    virtual void mutate() = 0;
    .
    .
    .
  };
  
  class GeneticAlgorithm
  {
    std::vector&lt;AbstractGenome*&gt; genomes;
  
    void init(int numGenomes)
    {
      for (int i=0; i &lt; numGenomes; ++i)
      {
        AbstractGenome *genome = new DerivedGenome(); &lt;=This is where my problem is
        genome-&gt;init();
        genomes.push_back(genome);
      }
    }
  
  }
  </code></pre>
  
  <p>where DerivedGenome is to be defined later (at some point) as:</p>
  
  <pre><code>class DerivedGenome: public AbstractGenome
  {
    void init() { do stuff; }
    void mutate() {do stuff; }
  }
  </code></pre>
  
  <p>My problem is that the only thing I know about DerivedGenome is that it derives from AbstractGenome - therefore I can't make a general call to the DerivedGenome constructor.</p>
  
  <p>One way I can think of solving this is to derive from GeneticAlgorithm and override the init function for all genome types, but I was wondering if there was a way to solve this in a nicer way, for example via templates?</p>
  
  <p>Thanks a lot in advance.   </p>