callback to a member function

Discussion in 'C++' started by Gert Van den Eynde, Nov 7, 2005.

  1. Hi all,

    What is "the best" way to have a callback from a C (or a Fortran) routine to
    a member function of a class? An example: I have objects of a class where
    double operator(double) is defined. I have a both a C and Fortran routine -
    that I cannot modify, nor convert to C or whatever, I'm stuck with them -
    that take a double f(double) function pointer as argument. These C and
    Fortran routines do a numerical quadrature using the object's operator().
    Hence, I need to provide this C function with a pointer to a member
    function. I've read the chapter in the C++ faq lite, I've come across the
    function pointer tutorial and read that and tested this approach (and it
    worked). But I was wondering if there is a more elegant way (no more global
    variables and such). I'm not really following TR1, but I heard something of
    a tr1::function, tr1::mem_fn etc. Can they be used to clean up the code?

    Thanks,
    gert
    Gert Van den Eynde, Nov 7, 2005
    #1
    1. Advertising

  2. Gert Van den Eynde wrote:
    > ...
    > What is "the best" way to have a callback from a C (or a Fortran) routine to
    > a member function of a class? An example: I have objects of a class where
    > double operator(double) is defined. I have a both a C and Fortran routine -
    > that I cannot modify, nor convert to C or whatever, I'm stuck with them -
    > that take a double f(double) function pointer as argument. These C and
    > Fortran routines do a numerical quadrature using the object's operator().
    > Hence, I need to provide this C function with a pointer to a member
    > function. I've read the chapter in the C++ faq lite, I've come across the
    > function pointer tutorial and read that and tested this approach (and it
    > worked). But I was wondering if there is a more elegant way (no more global
    > variables and such). I'm not really following TR1, but I heard something of
    > a tr1::function, tr1::mem_fn etc. Can they be used to clean up the code?
    > ...


    Unfortunately, no. There's no standard and portable way to turn an
    'object + member function pointer' pair into a regular function pointer
    in C++.

    Some implementations provide a non-standard functionality called
    'closure' that does exactly that (see Borland's C++ compiler for
    example). It can also be implemented in plaform-dependent way by using
    assembly language, for example. But there's no way to do this in pure C++.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 7, 2005
    #2
    1. Advertising

  3. Gert  Van den Eynde

    peter koch Guest

    Gert Van den Eynde skrev:

    > Hi all,
    >
    > What is "the best" way to have a callback from a C (or a Fortran) routine to
    > a member function of a class? An example: I have objects of a class where
    > double operator(double) is defined. I have a both a C and Fortran routine -
    > that I cannot modify, nor convert to C or whatever, I'm stuck with them -
    > that take a double f(double) function pointer as argument. These C and
    > Fortran routines do a numerical quadrature using the object's operator().
    > Hence, I need to provide this C function with a pointer to a member
    > function. I've read the chapter in the C++ faq lite, I've come across the
    > function pointer tutorial and read that and tested this approach (and it
    > worked). But I was wondering if there is a more elegant way (no more global
    > variables and such). I'm not really following TR1, but I heard something of
    > a tr1::function, tr1::mem_fn etc. Can they be used to clean up the code?
    >
    > Thanks,
    > gert


    You can't as the memberfunction needes two arguments - the double and
    the this-pointer. One way to come around this problem is to store (a
    pointer to) the object in global data and then use that for your
    function, but I agree that this is a kludge.

    /Peter
    peter koch, Nov 7, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prettysmurfed
    Replies:
    6
    Views:
    686
    prettysmurfed
    Jul 22, 2003
  2. Chris Morley

    Member Function Callback

    Chris Morley, Sep 14, 2004, in forum: C++
    Replies:
    2
    Views:
    565
    Uwe Schnitker
    Sep 15, 2004
  3. JDT
    Replies:
    6
    Views:
    527
    James Kanze
    Mar 29, 2007
  4. Ramesh
    Replies:
    11
    Views:
    4,184
    James Kanze
    Dec 27, 2008
  5. Angus
    Replies:
    3
    Views:
    679
    James Kanze
    Mar 2, 2009
Loading...

Share This Page