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
Re: Very Simple C++ Wrapper :: HELP
Quote:
Originally Posted by
Assoul
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));
}
};