How to pass C++ member function to C function expecting functionpointer?

R

RHS

I'm just starting to use C++, and don't want to have to rewrite all of
the old C routines that I've been using for 20+ years. One of these is
a general purpose function minimizer, which expects to be passed a
pointer to the function to be minimized,

double func(double p[])

How do I use this to minimize a function defined within a class in C+
+? To be specific, suppose I define the following class, which returns
the sum of squared differences between an input vector and a (private)
vector called trueParms:

class max_func {
public:
max_func(vector <double> trueParms_) : trueParms(trueParms_) {};
double operator()(double p[]);
private:
vector <double> trueParms;
};

double max_func::eek:perator () (double p[])
{
unsigned int i;
double temp;
double ss;
for (i=0, ss=0.0; i< trueParms.size(); i++) {
temp = p - trueParms;
ss += temp * temp;
}
return ss;
};

In the main code, if I have a vector v containing the values 1.0, 2.0,
3.0, then

max_func ff(v);

now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2)
^2+ (x[2] - 3)^2

How do I create a function pointer, say ffpointer, in my C++ code that
refers to ff, and which I can pass to my C minimizer?

Thanks for any suggestions.

Richard Stanton
 
A

Alf P. Steinbach

* RHS:
I'm just starting to use C++, and don't want to have to rewrite all of
the old C routines that I've been using for 20+ years. One of these is
a general purpose function minimizer, which expects to be passed a
pointer to the function to be minimized,

double func(double p[])

This is not a function taking a function as argumentn. And the name 'func' says
nothing. What is this?

How do I use this to minimize a function defined within a class in C+
+?

What minimize function?

You haven't shown one.

To be specific, suppose I define the following class, which returns
the sum of squared differences between an input vector and a (private)
vector called trueParms:

class max_func {
public:
max_func(vector <double> trueParms_) : trueParms(trueParms_) {};
double operator()(double p[]);
private:
vector <double> trueParms;
};

double max_func::eek:perator () (double p[])
{
unsigned int i;
double temp;
double ss;
for (i=0, ss=0.0; i< trueParms.size(); i++) {
temp = p - trueParms;
ss += temp * temp;
}
return ss;
};

In the main code, if I have a vector v containing the values 1.0, 2.0,
3.0, then

max_func ff(v);

now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2)
^2+ (x[2] - 3)^2

How do I create a function pointer, say ffpointer, in my C++ code that
refers to ff, and which I can pass to my C minimizer?


Depend on the minimizer.

Which you haven't shown.

Please grok this:

WE ARE NOT TELEPATHIC.

There's no way that we can "see" something that you neglect to write but only
think about.

Thanks for any suggestions.

Richard Stanton

Suggestion: read the FAQ items about how to post.

There's much good advice there.

The rest of the FAQ is also good.


Cheers & hth.,

- Alf
 
R

RHS

To clarify, here's the prototype for the (C) minimizing function:

void dfpmin(double p[], int n, double ftol, int *iter, double *fret,
double (*func)(double *), void (*dfunc)(double *, double *),
double *xscale);

Argument 6 is the pointer to the function to be minimized.

* RHS:
I'm just starting to use C++, and don't want to have to rewrite all of
the old C routines that I've been using for 20+ years. One of these is
a general purpose function minimizer, which expects to be passed a
pointer to the function to be minimized,
double func(double p[])

This is not a function taking a function as argumentn. And the name 'func' says
nothing. What is this?
How do I use this to minimize a function defined within a class in C+
+?

What minimize function?

You haven't shown one.




To be specific, suppose I define the following class, which returns
the sum of squared differences between an input vector and a (private)
vector called trueParms:
class max_func {
public:
    max_func(vector <double> trueParms_) : trueParms(trueParms_) {};
    double operator()(double p[]);
private:
    vector <double> trueParms;
};
double max_func::eek:perator () (double p[])
{
    unsigned int i;
    double temp;
    double ss;
    for (i=0, ss=0.0; i< trueParms.size(); i++) {
        temp = p - trueParms;
        ss += temp * temp;
    }
    return ss;
};

In the main code, if I have a vector v containing the values 1.0, 2.0,
3.0, then
max_func ff(v);
now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2)
^2+ (x[2] - 3)^2
How do I create a function pointer, say ffpointer, in my C++ code that
refers to ff, and which I can pass to my C minimizer?

Depend on the minimizer.

Which you haven't shown.

Please grok this:

WE ARE NOT TELEPATHIC.

There's no way that we can "see" something that you neglect to write but only
think about.


Thanks for any suggestions.
Richard Stanton

Suggestion: read the FAQ items about how to post.

There's much good advice there.

The rest of the FAQ is also good.

Cheers & hth.,

- Alf- Hide quoted text -

- Show quoted text -
 
S

sebastian

To clarify, here's the prototype for the (C) minimizing function:

 void dfpmin(double p[], int n, double ftol, int *iter, double *fret,
      double (*func)(double *), void (*dfunc)(double *, double *),
      double *xscale);

Argument 6 is the pointer to the function to be minimized.

Problem is, a class member function takes an additional 'this'
parameter, so the bottom line is that it's incompatible. Your best
bet, then, would be to either:
1) Declare a global instance of the class, and write a "plain-vanilla"
function to be passed to the routine that internally references the
variable. This works, but it's a pretty ugly solution, admittedly.
2) Bite the bullet, and C++ize your routines. This isn't as hard as it
sounds, actually. For example:

template <typename Func, typename Dfunc>
void dfpmin(double p[], int n, double ftol, int *iter, double *fret,
Func func, Dfunc dfunc, double *xscale);

Voila! Now it can be used with function and (most) objects,
interchangeably. Pretty simple, no?

Cheers.
 
J

John H.

Problem is, a class member function takes an additional 'this'
parameter, so the bottom line is that it's incompatible. Your best
bet, then, would be to either:
1) Declare a global instance of the class, and write a "plain-vanilla"
function to be passed to the routine that internally references the
variable. This works, but it's a pretty ugly solution, admittedly.
2) Bite the bullet, and C++ize your routines. This isn't as hard as it
sounds, actually. For example:

template <typename Func, typename Dfunc>
void dfpmin(double p[], int n, double ftol, int *iter, double *fret,
Func func, Dfunc dfunc, double *xscale);

Agreed.

With respect to the first method, the C++ FAQ has more to say on this:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html

With respect to the second method:
All that needs to be done is to change the function signature. The
body of the function remains the same, and existing code that already
calls it remains the same.
std::mem_fun and std::bind1st might be handy if you go this route.
 

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,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top