【发布时间】:2012-04-03 16:14:18
【问题描述】:
以下是我想在 3rd 方库中使用的类的构造函数(因此不能选择更改此函数)。
template <class Space>
moveset<Space>::moveset(particle<Space> (*pfInit)(rng*),
void (*pfNewMoves)(long, particle<Space> &,rng*),
int (*pfNewMCMC)(long,particle<Space> &,rng*))
但是,我需要每个函数都知道各种额外的信息,而不是简单地定义 3 个全局函数,显然我无法传递这些信息,因为没有输入参数。为了使问题进一步复杂化,我将要创建此 moveset 对象的几个不同实例,每个实例都希望使用相同的函数,但使用不同的底层数据。
我的想法是按照这些思路创建一个控股类,
Class DataPlusFunctions {
public:
DataPlusFunctions(Data* dataPtr) { dataPtr_ = dataPtr ;}
smc::particle<cv_state> fInitialise(smc::rng *pRng)
{
// the actual function will be a lot more complicated than this and
// likely to require calling other methods / classes.
// The Data stored in a different class will be changing...which is
// important in relation to the pfNewMoves function.
double value = dataPtr_->value() ;
return smc::particle<cv_state>(value,likelihood(0,value));
}
... same for other required functions
private:
Data* dataPtr_ ;
}
*
Class MainClass {
...
void IK_PFController::initialise()
{
std::vector<DataPlusFunctions> dpfV ;
for (int i = 0 ; i < NSAMPLERS ; i++)
dpfV.push_back(DataPlusFunctions(&data[i])) ;
pSamplers_ = (smc::sampler<cv_state>**)(new void* [NSAMPLERS]) ;
for (int i = 0 ; i < NSAMPLERS ; i++) {
// Normal way of calling function, having defined global functions e.g.
//smc::moveset<cv_state> Moveset(fInitialise, fMove, NULL);
// How to achieve this given my problem ??????????????
//smc::moveset<cv_state> Moveset(&dpfV[i]::fInitialise, &dpfV[i]::fMove, NULL);
pSamplers_[i].SetMoveSet(Moveset);
}
}
}
允许吗?如果没有,考虑到我将能够更改 moveset 类,是否有可能实现我正在尝试的目标?
【问题讨论】:
-
看看
boost::bind。 -
您能否提供更多信息,具体说明它将如何帮助我解决所描述的问题。
标签: c++ class templates member-function-pointers