Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to pass C++ member function to C function expecting functionpointer?

Reply
Thread Tools

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

 
 
RHS
Guest
Posts: n/a
 
      01-13-2010
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:perator () (double p[])
{
unsigned int i;
double temp;
double ss;
for (i=0, ss=0.0; i< trueParms.size(); i++) {
temp = p[i] - trueParms[i];
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-13-2010
* 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:perator () (double p[])
> {
> unsigned int i;
> double temp;
> double ss;
> for (i=0, ss=0.0; i< trueParms.size(); i++) {
> temp = p[i] - trueParms[i];
> 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
 
Reply With Quote
 
 
 
 
RHS
Guest
Posts: n/a
 
      01-13-2010
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.

On Jan 12, 4:46*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * 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:perator () (double p[])
> > {
> > * * unsigned int i;
> > * * double temp;
> > * * double ss;
> > * * for (i=0, ss=0.0; i< trueParms.size(); i++) {
> > * * * * temp = p[i] - trueParms[i];
> > * * * * 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 -


 
Reply With Quote
 
sebastian
Guest
Posts: n/a
 
      01-13-2010
On Jan 12, 5:00*pm, RHS <(E-Mail Removed)> wrote:
> 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.


 
Reply With Quote
 
John H.
Guest
Posts: n/a
 
      01-13-2010
On Jan 12, 7:25*pm, sebastian <(E-Mail Removed)> wrote:
> 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-lit...o-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Passing a vector to a function expecting an array alamaison C++ 20 01-15-2009 10:05 PM
Function expecting char*, does using vector<char> gain me anything? Jim Langston C++ 4 11-14-2006 10:54 AM
passing arg char[X][Y] to function expecting (char**) MackS C Programming 11 01-31-2005 07:31 AM
Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array Jonathan Underwood C Programming 2 08-13-2003 04:21 PM



Advertisments