I'm trying to adjust some math code that I wrote to allow arbitrary functions, but I can only do this by pre-defining them at compile time, which seems awkward. I am currently using function pointers, but as far as I can see, the same problem would occur with functors. To provide a simplified example, the following code is used for forward difference differentiation:
double xsquared(double x) {
return x*x;
}
double expx(double x) {
return exp(x);
}
double forward(double x, double h, double (*af)(double)) {
double answer = (af(x+h)-af(x))/h;
return answer;
}
If one of the first two functions can be passed as the third argument. However, I would like the user to enter the user password (in actual C ++), instead of having to pre-configure the functions. Any help would be greatly appreciated!