I'm have some difficulties with function pointers. I have an base class which defines a function pointer that via typedef double (*function)(double *x) const;
A quick side question: why does the above typedef not compile?
Gives the following error: error: ‘const’ and ‘volatile’ function specifiers on ‘function’ invalid in type declaration
For the part below I use typedef double (*function)(double *x). Now each daughter class can implement multiple and different versions of functions of this type. Via an enum I select the function of my choice, which sets my non-member function pointer (defined in the base class) to be initialized by one of these member-function pointers of the daughter class. Here's a code snippet:
The source file of the daughter class:
PndLmdROOTDataModel1D::PndLmdROOTDataModel1D(interpolation_type intpol_type) {
if(intpol_type == CONSTANT) {
setModelFunction(&PndLmdROOTDataModel1D::evaluateConstant);
}
else if (intpol_type == SPLINE) {
setModelFunction(&PndLmdROOTDataModel1D::evaluateSpline);
}
else {
setModelFunction(&PndLmdROOTDataModel1D::evaluateLinear);
}
}
And the base Class (header file):
class MultiModel1D: public Model1D {
protected:
function model_func;
public:
MultiModel1D();
virtual ~MultiModel1D();
void setModelFunction(function f);
}
When compiling I get the following error:
note: no known conversion for argument 1 from ‘double (PndLmdROOTDataModel1D::*)(double*)’ to ‘function {aka double (*)(double*)}’
I'm using the function pointer, because of speed issues (at least I think this should be faster than constantly running through some switch case). What am I doing wrong? Maybe there is also some design pattern that will serve as a better alternative... Thanks in advance!
Steve