In case of anyone needs the answer, I found it using http://www.newty.de/fpt/callback.html
Here is a code to answer my question, you can also find it at
https://stackoverflow.com/questions/62904534/integrate-a-public-but-non-static-member-function-with-alglib/62905919#62905919Code:
#include <functional>
#include <iostream>
#include <cmath>
#include "integration.h"
class Foo;
class Bar;
void* pt2Object; // global variable which points to an arbitrary object
/*****************************
Class Foo
*****************************/
class Foo
{
public:
// Constructor
Foo(Bar *b, int toto);
// Function to integrate
void f(double x, double xminusa, double bminusx, double &y, void *ptr);
// Wrapper
static void Wrapper_To_Call_f(double x, double xminusa, double bminusx, double &y, void *ptr);
private:
Bar *m_bar;
int m_toto;
};
Foo::Foo(Bar *b, int toto) : m_bar(b), m_toto(toto)
{}
void Foo::f(double x, double xminusa, double bminusx, double &y, void *ptr)
{
double *param = (double *) ptr;
double p1 = param[0];
double p2 = param[1];
y = exp(this->m_toto*x)/(p1 * p2);
// y = exp(x)/(p1 * p2);
}
void Foo::Wrapper_To_Call_f(double x, double xminusa, double bminusx, double &y, void *ptr)
{
// explicitly cast global variable <pt2Object> to a pointer to TClassB
// warning: <pt2Object> MUST point to an appropriate object!
Foo* mySelf = (Foo*) pt2Object;
// call member
mySelf->f(x, xminusa, bminusx, y, ptr);
}
/*****************************
Class Bar
*****************************/
class Bar
{
friend Foo;
public:
// Constructor
Bar();
private:
int m_a, m_b;
};
Bar::Bar() : m_a(2), m_b(5)
{}
/*****************************
Main program
*****************************/
int main(int argc, char *argv[])
{
Bar* b = new Bar();
// Create Foo
Foo myFoo(b, 1);
// Assign global variable which is used in the static wrapper function
// important: never forget to do this!!
pt2Object = (void*) &myFoo;
double arrayParams[2] = {1, 2};
double (*params)[2] = &arrayParams;
alglib::autogkstate s;
double v;
alglib::autogkreport rep;
alglib::autogksmooth(0, 1, s);
alglib::autogkintegrate(s, Foo::Wrapper_To_Call_f, params);
alglib::autogkresults(s, v, rep);
std::cout << v << std::endl;
return 0;
}