question about function pointers

A

amitmehrotra007

I have an old fortran library which has a function f1 which expects a
function pointer to be passed to it of the type
double(*) (const double &)

I have a class Foo which has two functions
class Foo
{
public:
double f2 (const double &);
void f3 ();
};
From within f3(), I want to pass Foo::f2 as an argument to my Fortran
function f1. What is the cleanest way of doing this? Is mem_fun
applicable in this case?
 
B

Ben Radford

I have an old fortran library which has a function f1 which expects a
function pointer to be passed to it of the type
double(*) (const double &)

I have a class Foo which has two functions
class Foo
{
public:
double f2 (const double &);
void f3 ();
};

function f1. What is the cleanest way of doing this? Is mem_fun
applicable in this case?

Since you are asking about mem_fun I assume you know that f2's type is
different from what f1 requires. Specifically double(Foo::*)(const
double&) instead of double(*)(const double&). I'm not an STL expert but
after a quick check of the mem_fun documentation I don't think it
applies to your case. It can be used as an adapter to call member
functions as if they were ordinary functions only when they have no
parameters.
 
A

amitmehrotra007

I think mem_fun can be used for member functions with no arguments or
one arguments.
 
A

Alf P. Steinbach

* (e-mail address removed):
I have an old fortran library which has a function f1 which expects a
function pointer to be passed to it of the type
double(*) (const double &)

I have a class Foo which has two functions
class Foo
{
public:
double f2 (const double &);
void f3 ();
};

From within f3(), I want to pass Foo::f2 as an argument to my Fortran
function f1. What is the cleanest way of doing this?

Depends on whether you have to take threading into consideration or not.

If no threading, then

void f3()
{
static Foo* currentSelf;

struct Static
{
static double callback( double const& d )
{
return currentSelf->f2( d );
}
};

currentSelf = this; fortranFunc( &Static::callback );
}

Is mem_fun applicable in this case?

No, it produces a functor object, not a function.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top