For structural reasons, I’d like to be able to pass an instance of a functor to another functor. At the moment I achieve something equivalent by handing a pointer to a function to my functor.
I’ve attempted to encapsulate the idea in some minimal code below:
class A
{
private:
double _x, _y, _z;
public:
A (double x, double y, double z) : _x(x), _y(y), _z(z) {};
void operator() (double t) const
{
// Some stuff in here that uses _x, _y, _z, and t.
}
};
class B
{
private:
// What is the type of the functor instance?
??? A ???
public:
// How do I pass the instance of A into B at initialisation?
B (??? A ???) : ??? : {};
void operator() (double tau) const
{
// Something that uses an instance of A and tau.
}
};
int main(void)
{
// I want to do something like this:
A Ainst(1.1, 2.2, 3.3); // Instance of A.
B Binst(Ainst); // Instance of B using instance of A.
Binst(1.0); // Use the instance of B.
return 0
}
In essence, I want to be able to chain up functors. As stated above, I currently do this by passing a function pointer to B along with variables x, y, and z. In my code B is templated and the goal is to write it once and then reuse it without any modification later, which means that handing x, y, and z to B is not ideal. A on the other hand will be customised for each program that I write. I don’t mind B being quite messy, but I want A to be nice and clean as this is the part that will be exposed.
For those who know some quantum mechanics, B is the Schrödinger equation (or a master equation) and A is a time dependent Hamiltonian. The variables x, y, and z are used to construct the Hamiltonian, and t is time, allowing me to use the odeint library (so I’m using ublas and a couple of other Boost bits).
Using references?