I'm working on the implementation of a genetic algorithm that is supposed to work on an abstract genome type.
My setup is something like this:
class AbstractGenome
{
virtual void init() = 0;
virtual void mutate() = 0;
.
.
.
};
class GeneticAlgorithm
{
std::vector<AbstractGenome*> genomes;
void init(int numGenomes)
{
for (int i=0; i < numGenomes; ++i)
{
AbstractGenome *genome = new DerivedGenome(); <=This is where my problem is
genome->init();
genomes.push_back(genome);
}
}
}
where DerivedGenome is to be defined later (at some point) as:
class DerivedGenome: public AbstractGenome
{
void init() { do stuff; }
void mutate() {do stuff; }
}
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.
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?
Thanks a lot in advance.