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

Discussion in 'C++' started by RHS, Jan 13, 2010.

  1. RHS

    RHS Guest

    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
    RHS, Jan 13, 2010
    #1
    1. Advertising

  2. * 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
    Alf P. Steinbach, Jan 13, 2010
    #2
    1. Advertising

  3. RHS

    RHS Guest

    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" <> 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::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 -
    RHS, Jan 13, 2010
    #3
  4. RHS

    sebastian Guest

    On Jan 12, 5:00 pm, RHS <> 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.
    sebastian, Jan 13, 2010
    #4
  5. RHS

    John H. Guest

    On Jan 12, 7:25 pm, sebastian <> 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-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.
    John H., Jan 13, 2010
    #5
    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. Jonathan Underwood
    Replies:
    2
    Views:
    615
    Simon Biber
    Aug 13, 2003
  2. Andreas Klimas

    functionpointer prototype with void* : question

    Andreas Klimas, Sep 15, 2006, in forum: C Programming
    Replies:
    4
    Views:
    299
    Keith Thompson
    Sep 15, 2006
  3. humblemark

    parameter and functionpointer

    humblemark, Nov 22, 2007, in forum: C Programming
    Replies:
    6
    Views:
    266
    Justin Spahr-Summers
    Nov 23, 2007
  4. news.aioe.org
    Replies:
    2
    Views:
    264
    diamondback
    Oct 7, 2008
  5. Anonimo
    Replies:
    0
    Views:
    286
    Anonimo
    Nov 18, 2009
Loading...

Share This Page