class MyAbstractClass {
virtualint getFitness(Gene gene) = NULL;
};
This class is used by another class to .. calculate fitness for its data. Now this data can vary over different classes that may need to calculate fitness, so I <want> their fitness calculating functions to be varied as well. Like this:
1 2 3 4 5 6
class MyFitnessClass : public MyAbstractClass {
virtualint getFitness(Gene_A gene);
};
class MyOtherFitnessClass : public MyAbstractClass {
virtualint getFitness(Gene_B gene);
};
In this case, Gene_A and Gene_B are subclasses of Gene.
Obviously, this won't work, since MyFitnessClass and MyOtherFitnessClass need to implement the getFitness method with [b]Gene[b] as argument.
I can't figure out how to do this neatly. I could give the method a pointer as argument, but that wouldn't be type safe. I would like it to be. Is there a way to have MyAbstractClass have its argument be a type that can be defined per subclass?
template <class T>
class method_holder{
public:
virtualint get_fitness(T);
};
class foo:
public if_you_need_a_base_class,
private method_holder<Gene_B>
{
public:
using method_holder<Gene_B>::get_fitness; //if you want default behaviour
virtualint get_fitness(Gene_B); //if you want to override
};
class MyFitnessClass : public MyAbstractClass {
virtualint getFitness(Gene gene);
};
class MyOtherFitnessClass : public MyAbstractClass {
virtualint getFitness(Gene gene);
};
Since Gene_A and Gene_B are subclasses of Gene, this should work. And the behavior of the getFitness methods can vary between the classes.
That is a bit of an unusual declaration of a pure virtual function.
Normally it looks like this:
virtualint getFitness(Gene gene) = 0;
Even though NULL might evaluate to 0, the problem is that NULL is a C language construct, and can mean 0 or a void pointer. So NULL is not recommended for C++ programs. C++ has nullptr, but that is an entirely different thing to what you want, so just stick to 0 for pure virtual functions. All the documentation I have ever seen uses 0, just wondering why someone would want to change that?
class MyFitnessClass : public MyAbstractClass {
virtualint getFitness(Gene gene);
};
class MyOtherFitnessClass : public MyAbstractClass {
virtualint getFitness(Gene gene);
};
Since Gene_A and Gene_B are subclasses of Gene, this should work. And the behavior of the getFitness methods can vary between the classes.
No, it shouldn't work. The type of the parameters is Gene. It will never be Gene_A or Gene_B. One must use either a pointer or reference to preserve the type of the parameter.