Connecting Tech Pros Worldwide Help | Site Map

callback to a member function

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 7th, 2005, 07:35 AM
Gert Van den Eynde
Guest
 
Posts: n/a
Default callback to a member function

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

  #2  
Old November 7th, 2005, 08:25 AM
Andrey Tarasevich
Guest
 
Posts: n/a
Default Re: callback to a member function

Gert Van den Eynde wrote:[color=blue]
> ...
> 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?
> ...[/color]

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

  #3  
Old November 7th, 2005, 12:45 PM
peter koch
Guest
 
Posts: n/a
Default Re: callback to a member function


Gert Van den Eynde skrev:
[color=blue]
> 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[/color]

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.