T
tbringley
I am a c++ newbie, so please excuse the ignorance of this question.
I am interested in a way of having a class call a general member
function of another class.
Specifically, I am trying to write an ordinary differential equation
class that would solve a general equation in the form:
dx/dt = f(x,t).
The ode class shouldn't know anything about f, except how to call it.
My first thought was to have a data member that stores a pointer to
the function f like:
double (*f) (double, double);
However, I learned that f could not point to a non-static member
function of a different class. This is bad for me, because I need to
use the solver for functions that are complicated to evaluate and
depend on many parameters. A simple example:
class my_f{ public:
double a;
double f(double x, double t){ return t+x*a;}
};
my_f foo; foo.a = 7;
I would like the ode solver to call foo.f(x,t). I could make
double (my_f::* f)(double, double);
a member of the ode solver, but then I would have to write a new ode
solver for every new class of functions to be used for f or to have
all these derive from some kind of base class, which I would prefer to
avoid.
My question is: is there a simple and elegant way to do this? I would
think that similar issues have been encountered many times before.
Thanks.
I am interested in a way of having a class call a general member
function of another class.
Specifically, I am trying to write an ordinary differential equation
class that would solve a general equation in the form:
dx/dt = f(x,t).
The ode class shouldn't know anything about f, except how to call it.
My first thought was to have a data member that stores a pointer to
the function f like:
double (*f) (double, double);
However, I learned that f could not point to a non-static member
function of a different class. This is bad for me, because I need to
use the solver for functions that are complicated to evaluate and
depend on many parameters. A simple example:
class my_f{ public:
double a;
double f(double x, double t){ return t+x*a;}
};
my_f foo; foo.a = 7;
I would like the ode solver to call foo.f(x,t). I could make
double (my_f::* f)(double, double);
a member of the ode solver, but then I would have to write a new ode
solver for every new class of functions to be used for f or to have
all these derive from some kind of base class, which I would prefer to
avoid.
My question is: is there a simple and elegant way to do this? I would
think that similar issues have been encountered many times before.
Thanks.