非成员函数指针作为 API 中成员函数的回调 [英] non-member function pointer as a callback in API to member function
问题描述
我使用的 API 要求我将函数指针作为回调传递.我正在尝试在我的 C++ 类中使用此 API,但出现编译错误.
I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class in C++ but I'm getting compilation errors.
API 定义为:
typedef void (__stdcall *STREAM_CALLBACK)(void *userdata);
__declspec(dllimport) int __stdcall set_stream_callback(
STREAM_CALLBACK streamCB, void *userdata);
第三方提供的一个示例文件是:
One example file, provided by the third party, is:
void __stdcall streamCB(void *userdata)
{
// callback implementation
}
int main(int argc, const char argv[])
{
int mid = 0;
set_stream_callback(streamCB, &mid);
}
而且效果很好.但是,当我尝试在课堂上使用它时,出现错误:
And that works fine. However when I try to use that in a class, I have an error:
错误 C3867:'MyClass::streamCB':函数调用缺少参数列表;使用&MyClass::streamCB"创建指向成员的指针
error C3867: 'MyClass::streamCB': function call missing argument list; use '&MyClass::streamCB' to create a pointer to member
使用建议
&MyClass::streamCB
&MyClass::streamCB
不起作用.我知道 set_stream_callback 只接受非成员函数.
doesn't work. I understood that the set_stream_callback only accepts a non-member function.
这个问题非常类似于如何将类成员函数作为回调?其中约翰内斯提出了一个简洁的建议,但我不太明白.任何人都可以扩展一点,如果我是对的,它与这个问题有关吗?
The problem is very similar to How can I pass a class member function as a callback? in which Johannes makes a concise suggestion, however I do not understand it very well. Could anyone expand a bit, if I am correct that it is relevant to this question?
我试过了:
void __stdcall MyClass::streamCB(void *userdata)
{
// callback implementation
}
static void MyClass::Callback( void * other_arg, void * this_pointer ) {
MyClass * self = static_cast<ri::IsiDevice*>(this_pointer);
self->streamCB( other_arg );
}
//and in the constructor
int mid = 0;
set_stream_callback(&MyClass::Callback, &mid);
但是
错误 C2664:set_stream_callback":无法将参数 1 从'void (__cdecl *)(void *,void *)' 到 'STREAM_CALLBACK'
error C2664: 'set_stream_callback' : cannot convert parameter 1 from 'void (__cdecl *)(void *,void *)' to 'STREAM_CALLBACK'
我该如何解决这个问题?
How do I get around this?
Edit1:另外,我想在 streamCB
回调中使用 userdata
.
Also, I want to use userdata
inside the streamCB
callback.
推荐答案
您可以通过将 userdata 转换为 MyClass 的属性来实现您想做的事情.那么您就不必将它传递给 MyClass::Callback,这是不可能的,因为您只能传递一个参数,而且它将是对象实例.这是一个例子.
You can achieve what you want to do by turning the userdata into a property of MyClass. Then you don't have to pass it to MyClass::Callback, which would be impossible, since you can only pass one parameter, and it would be the object instance. Here's an example.
void __stdcall MyClass::streamCB()
{
// callback implementation
}
static void MyClass::Callback(void * this_pointer ) {
MyClass * self = static_cast<MyClass>(this_pointer);
self->streamCB();
}
MyClass::MyClass(void *userdata) {
// do whatever you need to do with userdata
// (...)
// and setup the callback at C level
set_stream_callback(&MyClass::Callback, (void *)this);
}
在您的示例中,int mid
变量将成为该类的属性,因此可以从回调实现 streamCB
访问.
In your example, the int mid
variable would become a property of that class, and thus be accessible from the callback implementation streamCB
.
这篇关于非成员函数指针作为 API 中成员函数的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!