指向成员函数的函数的指针 [英] Pointer to function to member function

查看:135
本文介绍了指向成员函数的函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用具有函数set_min_objective的库(nlopt),该函数需要一个指向数字函数myfunc的指针并找到其最小值.我想创建一个包含适当初始化的成员函数的类.然后,set_min_objective将在特定实例(以下示例中的myP)中找到最佳值.调用顺序为:

I want to use a library (nlopt) that has a function set_min_objective which takes a pointer to a numerical function myfunc and find its minimum. I would like to create a class that will contain a suitably initialized member function. set_min_objective would then find an optimum in a specific instance (myP in the example below). The call sequence is:

opt.set_min_objective(myfunc, NULL);

,我想使用类似的东西:

and I would like to use something like:

opt.set_min_objective(myP.f, NULL);

编译时出现的错误是:

main.cpp: In function 'int main()':
main.cpp:79:34: error: no matching function for call to 'nlopt::opt::set_min_objective(<unresolved overloaded function type>, NULL)'
../lib/nlopt.hpp:335:10: note: candidates are: void nlopt::opt::set_min_objective(double (*)(unsigned int, const double*, double*, void*), void*)
../lib/nlopt.hpp:342:10: note:                 void nlopt::opt::set_min_objective(double (*)(const std::vector<double>&, std::vector<double>&, void*), void*)
../lib/nlopt.hpp:368:10: note:                 void nlopt::opt::set_min_objective(double (*)(unsigned int, const double*, double*, void*), void*, void* (*)(void*), void* (*)(void*))

让set_min_objective接受myP.f作为函数的普通指针的最简单解决方案是什么?请注意,myP.f和myfunc具有相同的参数和返回值类型.

What would be the simplest solution to have set_min_objective accepts myP.f as a normal pointer to function ? Note that myP.f and myfunc have the same arguments and return value types.

谢谢

JD

推荐答案

您不能直接这样做.您正在尝试将成员函数指针作为函数指针传递.要做的方法是编写一个包装函数(普通函数),例如,将计算委托给对象的成员函数,例如,全局对象.

You can't do that directly. You are trying to pass a pointer-to-member-function as a pointer-to-function. The way to go is to write a wrapper function (a normal function) which, for example, delegates the calculation to member function of your object, which is, for example, a global object.

编辑

实际上,您很幸运:opt.set_min_objective的第二个参数是指向数据的指针,该数据将传递给您将指针作为第一个参数传递给函数.这意味着您的包装器函数不必使用全局对象.

Actually, you are lucky: the second parameter to opt.set_min_objective is a pointer to your data which will be passed to the function you pass pointer to as first parameter. This means that your wrapper function doesn't have to use a global object.

class YourClass
{
public:
    double f(unsigned int i, const double*, double*, void*);
};

double wrapper(unsigned int i, const double* a, double* b, void* o) {
    // not sure what you'd need the last param for, but you say you have it...
    reinterpet_cast<YourClass*>(o)->f(i, a, b, o);
}

然后:

YourClass myP;
opt.set_min_objective(wrapper, &myP);

另一个编辑.在您之前,其他人也遇到了类似的问题: http://www.cplusplus.com/forum/general /73166/

Another edit. Someone else had a similar problem before you: http://www.cplusplus.com/forum/general/73166/

这篇关于指向成员函数的函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆