非成员函数指针的API成员函数回调 [英] non-member function pointer as a callback in API to member function

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

问题描述

我使用的要求我传递一个函数指针作为回调的API。我试图使用此API从我在C级+ +,但我得到的编译错误。

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.

的问题是非常相似的
<一href=\"http://stackoverflow.com/questions/400257/how-can-i-pass-a-class-member-function-as-a-callback\">How我可以通过一个类的成员函数作为回调?
其中约翰内斯使得简洁的建议,但我不明白它非常好。任何人都可以扩大一点,如果我是正确的,这是有关这个问题的?

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从
  无效(__cdecl *)(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 回调在

Also, I want to use userdata inside the streamCB callback.

推荐答案

可以达到你想要通过转动用户数据到MyClass的的属性做什么。然后,你不必将它传递给MyClass的::回调,这将是不可能的,因为你只能传递一个参数,这将是对象实例。
下面是一个例子。

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中旬变量将成为类的属性,从而可以从回调实施 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屋!

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