由于结构原因,我希望能够将仿函数的实例传递给其他仿函数。目前,我通过将函数指针交给函子来实现相同的功能。将仿函数的实例传递给其他仿函数
我已经尝试封装的想法在下面的一些最少的代码:
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
}
从本质上讲,我希望能够以环比上涨函子。如上所述,我现在通过将函数指针与变量x,y和z一起传递给B来完成此操作。在我的代码B中是模板化的,目标是编写一次,然后在不做任何修改的情况下重用它,这意味着将x,y和z交给B并不理想。另一方面,A将针对我编写的每个程序进行定制。我不介意B很杂乱,但我希望A很好,很干净,因为这是会暴露的部分。对于那些知道一些量子力学的人来说,B是薛定谔方程(或主方程),A是时间依赖的哈密尔顿函数。变量x,y和z用于构造哈密尔顿算子,t是时间,允许我使用库(所以我使用ublas和其他几个Boost位)。
你需要std :: bind。它可以链接函数,并绑定参数。 – innochenti 2012-04-12 11:29:06