CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2010
    Posts
    2

    Exclamation Very Simple C++ Wrapper :: HELP

    Hi Guys,

    I know there is a lot to say on Wrapper and there are many threads but I really don't get it as a newbie in C++.

    I am trying to use a minimization algorithm called by:

    double Calibration::Calibrate() {
    ...
    CSimplex NMS(3,TOL,MAX_ITER,&functionToMinize);
    ...
    }

    The thing is functionToMinize is a member function and it has to take only a vector<double> as argument.

    So I thought of a Wrapper.

    Something like this:

    double Wrapper_To_Call_Calibration(vector<double> x)
    {
    return wrapp -> functionToMinize(x);
    }

    Obviously this doesn't work. How should I proceed?

    Create another class Wrapper with:
    attribut Calibration*
    constructor that initialize my pointor to my Calibration class.
    Then define a function Wrapper_To_Call_Calibration inside this class?

    I tried this but it doesn't work.

    Any help please? (but very simple help, I am not expert in C++)...

    Thanks a lot guys

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Very Simple C++ Wrapper :: HELP

    Quote Originally Posted by Assoul View Post
    Obviously this doesn't work. How should I proceed?
    It really depends on the argument type that the CSimplex constructor expects. If it takes a plain function pointer, then you can't get around using global/static data. I.e. you can make functionToMinize a static member function and any data it uses too.

    If CSimplex is under your control, I suggest you switch to using a boost::function (or std::function if your compiler supports it) instead of a function pointer. They are much more flexible and can easily be used with member functions.

    This code may contain errors, it's only to show the idea.
    Code:
    #include <functional>
    #include <vector>
    class CSimplex
    {
    public:
        CSimplex(int, int, int, std::function<double(std::vector<double>)>);
        // ...
    };
    
    class Calibration
    {
    public:
        double functionToMinimize(std::vector<double>);
        double Calibration::Calibrate() {
            CSimplex NMS(3, TOL, MAX_ITER,
                std::bind(&Calibration::functionToMinize, this));
        }
    };
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured