如何将类成员函数传递给第三方库中的方法? [英] How to pass class member functions to a method in a 3rd party library?
问题描述
以下是我想在第三方库中使用的类的构造函数(因此,不能修改此函数)
The following is the constructor of a class I would like to use in a 3rd party library (so altering this function isn't an option).
template <class Space>
moveset<Space>::moveset(particle<Space> (*pfInit)(rng*),
void (*pfNewMoves)(long, particle<Space> &,rng*),
int (*pfNewMCMC)(long,particle<Space> &,rng*))
<然而,不是简单地定义3个全局函数,我需要每个函数知道各种额外的信息,显然我不能通过,因为没有输入参数。为了进一步复杂的问题,我想要使这个moveet对象的几个不同的实例,每个想使用相同的功能,但在不同的底层数据。
However, rather than simply defining 3 global functions, I need each of the functions to know various extra information, which obviously I can't pass as there are no input arguments. To further complicate the issue, I am going to want to make several different instances of this moveset object, each wanting to use the same functions, but upon different underlying data.
我的想法是创建一个持有类沿着这些行,
My thought is to create a holding class something along these lines,
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);
}
}
}
b $ b
是允许的吗?
Is is allowed? If not, is it possible to achieve what I am attempting given that I will be able to alter the moveset class?
推荐答案
如果没有,是否可以实现我试图给出的为了调用成员函数(通过指针),你需要一个适当类型的对象。由于第三方函数需要使用vanilla函数指针,因此你不能传递成员函数。
In order to call a member function (via pointer), you need an object of the appropriate type. Since the 3rd party function requires vanilla function pointers, you cannot pass a member function.
你最好做的就是定义三个函数
The best you could do (AFAIK) is to define three functions
particle<Space> Init(rng*);
void NewMoves(long, particle<Space> &,rng*);
int NewMCMC(long,particle<Space> &,rng*);
并设置这些函数访问的全局变量。例如:
and set a global variable that those functions access. e.g.:
DataPlusFunctions* g = NULL;
particle<Space> Init(rng* r)
{
// g==NULL handling omitted
return g->fInitialise(r);
}
// similarly for the others
code> g 在调用第三方函数之前。
and set the value of g
before calling the 3rd party function.
优点是你有一个对象可以用来存储状态信息并且你可以用另一个(甚至可以使用接口)替换指向的对象,提供动态行为。
The advantage is that you have an object you can use to store state info and also you can replace the pointed-to object with another (maybe even using interface), providing dynamic behavior.
问题是如果你想在并行设置为全局可能会同时更改两个线程 - 在这种情况下,您可以使用互斥锁或锁来保护它。
The problem is if you want to use this in a parallel setting as the global might be changed by two threads simultaneously -- in this case you could protect it with a mutex or lock.
这篇关于如何将类成员函数传递给第三方库中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!