I need to pass a member function to gsl_function, and used the wrapper described in this [link] (A function pointer issue. How to efficiently interface with C API (ie. GSL) from C++ class?). It worked, but I am a bit confused when trying to understand it. Basically there are three void* params that I labeled ptr_par_0
, ptr_par_1
, ptr_par_2
in the code, it is obvious ptr_par_0=patr_par_1
but they seem never get initialized in the code, is that true? ptr_par_2
is indeed initialized to *this
, but ptr_par_0
& ptr_par_1
are local variables of foo_wrapper
function, so I think they should hide ptr_par_2
and need their own initialization. I think there might be something I am unaware of, thanks in advance for clarification.
class MyClass
{
double foo(double x)
{
...
}
static double foo_wrapper(double x, void *params) //ptr_par_0
{
return static_cast<MyClass*>(params)->foo(x); //ptr_par_1
}
double bar(double x)
{
...
gsl_function F;
F.function=&MyClass::foo_wrapper;
F.params=this; //ptr_par_2
// invoke GSL function passing in F
...
}
};