I want to create a general function which returns a function pointer to another function in C/C++. However, second returned function should be able to use variable from first function.
Example,
typedef double (*func_t)(double);
func_t inverse(func_t fn) {
// define another function here that uses fn
double solve(double x) {
// use fn
}
return &solve;
}
double sqr(double x) { return x * x; }
int main() {
func_t inv = inverse(sqr);
printf("sqrt %d = %f\n", 100, inv(100));
}
Obviously gcc, g++ do not allow me to do this. Can I achieve this without using classes or structs.