Original Post
Let's say that I have a class Simulation that contains a pointer to an integrator of type Integrator. The integrator class has a function "integrate" which takes a state and a function that can calculate the derivative of the state and returns the new state. The obvious way to implement this is with function pointers like this: But is there any more "c++" way of doing it such as functors? The problem is that the GetDerivatives function has to use other functions and information that're contained in "Simulation" (bodies,forces...). So if I made a "GetDerivatives" functor it'd have to hold a pointer to Simulation - and in that case I could just give "Integrator" a pointer to "Simulation" and let it use the function directly. It just seems like bad design to have objects point to each other. Are there better ways of implementing this? Thanks.
class Simulation
{
...
Integrator *integrator;
float[] GetDerivatives(float[] state, float dt);
void Update(float dt);
};
void Simulation :: Update(float dt)
{
integrator.integrate(..., &GetDerivatives);
}
class Integrator
{
...
float[] integrate(float[] state, float t0, float t1,
float[] (*GetDxDt)(float[] state, float dt));
};