问问题
225 次
1 回答
6
I presume the void*
in your function definition is a user-specified callback parameter. In that case, use this parameter to pass a pointer to your object and make your callback a static function. Inside this static function, cast this pointer back to the proper type (Experiment*
) and call the non-static version of the function.
class Experiment
{
public:
Experiment();
~Experiment();
void setupExperiment();
static int static_func(double t, const double y[], double f[], void *params);
static int static_jac (double t, const double y[], double *dfdy, double dfdt[], void *params);
virtual int func(double t, const double y[], double f[]);
virtual int jac (double t, const double y[], double *dfdy, double dfdt[]);
};
void Experiment::setupExperiment()
{
gsl_odeiv2_system sys = {static_func, static_jac, 2, this}; //Here is the problem with virtual functions
}
int Experiment::static_func(double t, const double y[], double f[], void *params)
{
return ((Experiment*)params)->func(t, y, f);
}
int Experiment::static_jac (double t, const double y[], double *dfdy, double dfdt[], void *params)
{
return ((Experiment*)params)->jac(t, y, dfdy, dfdt);
}
class aSpecificProblem: public Experiment
{
public:
virtual int func(double t, const double y[], double f[]);
virtual int jac (double t, const double y[], double *dfdy, double dfdt[]);
};
于 2012-05-21T14:45:21.757 回答