由于结构上的原因,我希望能够将一个functor实例传递给另一个functor。目前,我通过将指向函数的指针传递给我的functor来实现相同的功能。
我尝试将这个想法封装在下面的一些最小代码中:
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并不理想。另一方面,我将为我编写的每个程序定制。我不介意B是相当凌乱,但我希望A是好的和干净的,因为这是将被曝光的部分。
对于那些知道一些量子力学的人来说,B是Schrödinger方程(或主方程),A是依赖时间的哈密顿量。变量x、y和z用于构造哈密顿量,t是时间,允许我使用odeint库(所以我使用ublas和其他几个Boost位)。
发布于 2012-04-12 19:08:27
使用引用?
class A { /* ... */ };
class B
{
A &a;
public:
B(const A &my_a)
: a(my_a)
{ }
// ...
};https://stackoverflow.com/questions/10122285
复制相似问题