I'm trying to build a generic algorithm. So far I have achieved this using class hierarchy and pointers, as in the example below:
struct Base{
virtual double fn(double x){return 0;}
};
class Derived : public Base{
double A;
public:
Derived(double a) : A(a) {}
double fn(double x) { return A*x;}
};
//Some other implementations
class algo{
double T;
std::unique_ptr<Base> b_ptr;
public:
algo(double t, std::unique_ptr<Base>& _ptr); //move constructor...
//Some constructors
double method(double x){ return T*b_ptr->fn(x);}
};
This set up is then implemented as follows:
int main(){
std::unique_ptr<Derived> ptr(new Derived(5.4));
algo(3.2,ptr);
method(2.4);
return 0;
}
This is a very simple example, of course, but it serves for my question. From what I understand, using derived classes in this way means that the method is chosen at run-time rather than at compile time. Since I do not need any dynamic behaviour from my algorithm - everything is determined at compile time - this is a needless loss of efficiency. Is there a way to do the above at compile time, i.e. static polymorphism?
From what I understand, it's only possible to get static polymorphism using templates. I haven't been able to find a implement templates with non-primitive types, however. As in the example above, I need derived classes with non-default constructors, which doesn't seem to be possible... Could anyone offer any solutions as to how this might be done?
it's only possible to get static polymorphism using templates
Polymorphism is about call the correct function base on current object type. So I'd like to say function overload is also static polymorphism.