I need to convert a call to a function from dynamic polymorphism
to static polymorphism
but I'm not pretty sure on how this transformation must be done.
As first thing I have a class called Calculator that can have different implementations :
template<class T>
class Calculator {
public:
virtual void doSomething() = 0;
};
class IntCalculator : Calculator<int> {
public:
int data;
void doSomething(){ ... use data .. };
};
class CharCalculator : Calculator<char> {
public:
void doSomething(){ .. };
};
Then I have another class that receive as input a calculator and use it:
class Matrix {
private:
Calculator* calc;
public:
void set_calculator(Calculator* calc){
calculator = calc;
}
void run(){
calculator->doSomething();
}
}
Now I want to convert this function call in such a way that use static polymorphism but it's not clear to me how can I do this transformation. In case of IntCalculator
the attribute data must be loaded before passing the calculator
to the Matrix
.
Any suggestions?
template <typename T>
class Matrix {
private:
T* calc;
public:
void set_calculator(T* calc){ calculator = calc; }
void run(){ calculator->doSomething(); }
};
Then you no longer need Calculator
interface, and can directly use IntCalculator
.