0

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
        ...
    }
};
Community
  • 1
  • 1
lychee10
  • 15
  • 1
  • 4
  • 1
    ptr_par_0 is a function parameter. It is initialized when the function gets called. – cpplearner Jul 17 '15 at 15:04
  • GSL will call something like `F.function(x, F.params);` so `MyClass::foo_wrapper(x, this_from_bar)`. – Jarod42 Jul 17 '15 at 15:07
  • 1
    If you're still learning C++, GSL is **way** too hard. And if you don't understand yet how function parameters are initialized, then you're definitely still learning C++. – MSalters Jul 17 '15 at 15:41
  • Thank you Jarod42, right to the point! I found the GSL_FN_EVAL(&F,x) calling method you pointed out. I would accept it if you post it as an answer. – lychee10 Jul 17 '15 at 15:59

1 Answers1

1

Turn comment into answer:

GSL will call something like F.function(x, F.params); (in GSL_FN_EVAL(&F,x))

so MyClass::foo_wrapper(x, this_from_bar)

Jarod42
  • 203,559
  • 14
  • 181
  • 302