我遇到了函数指针的问题,并且我在网络上找到的任何东西都帮助我解决了这个问题。如何从成员函数中获取“简单”函数指针
我有选自C API,它需要一个void函数的指针的函数:
extern int APIFunction(int, void (*func)(int));
我有,我想,当我调用API函数把函数的类。
class MyClass
{
public:
void myFunction(int status, otherAPi arguments...);
};
然后,我创建了一个指向我的会员功能,创建了类的新实例
typedef void (MyClass::*MyClassFunctionPointer)(int stat, otherAPi arguments...);
MyClassFunctionPointer fctPointer= &MyClass::myFunction;
LicenseSecurity instance;
我得到一个错误,当我尝试打电话给我的API函数与我创建的函数指针:
E2034 Impossible to convert 'void (MyClass::*)(int, otherAPITypes...)' into 'void (*) (int, otherAPITypes...)'
E2342 Bad type correspondence in the parameter 'func' ('void (*)(int, otherAPITypes...)' desired, 'void(int, otherAPITypes...)' obtained)
:
int stat = APIFunction(5, fctPointer); // -> error 1
int stat = APIFunction(5, instance.*fctPointer); // -> error 2
我在第一和第二种情况分别得到了错误
我无法访问API函数,因此我无法修改它。为了总结这个问题:如何获得一个“简单的”C函数指针来放入我的类的成员函数的函数参数?
谢谢
我认为boost :: bind在这里很有帮助 – Constantin 2013-04-11 20:49:33
@Constantin然后他会有一个包装成员函数调用的函子......但他仍然无法将它传递给API函数。 – 2013-04-11 20:55:01
[C++类成员函数回调]可能的重复(http://stackoverflow.com/questions/8079453/c-class-member-function-callback) – 2013-04-11 20:55:16