简体   繁体   English

C ++回调函数

[英]C++ Callback function

I'm setting a timer using afxwin SetTimer function. 我正在使用afxwin SetTimer函数设置一个计时器。 This function receives three params one of which is a callback function. 该函数接收三个参数,其中一个是回调函数。

I set a callback function in a class (let's call it NS) and tried to pass it as an argument. 我在类中设置了一个回调函数(让我们称之为NS)并尝试将其作为参数传递。 However the compiler complains that this: 然而编译器抱怨这个:

void (__stdcall NS::*) (HWND, UINT, UINT_PTR, DWORD);

is not the same as: 是不一样的:

void (__stdcall *) (HWND, UINT, UINT_PTR, DWORD);

What should I do? 我该怎么办?

Make the function static *. 使功能static *。 Otherwise it's not even a function, but rather a member function, which is a very different animal. 否则它甚至不是一个功能,而是一个成员功能,这是一个非常不同的动物。 Moreover, it doesn't otherwise make sense to "pass the function in a class as a callback", as you put it: A non-static member function is tied to the state of an object instance of that class. 此外,将“将函数作为回调传递给函数”没有意义,正如您所说的那样:非静态成员函数与该类的对象实例的状态相关联。

(Also, search this site, this question has been asked a million times over and there are many good alternatives spread out throughout SO.) (此外,搜索这个网站,这个问题已被问过一百万次,并且在整个SO中有很多好的选择。)

*) As @James points out, this is not sufficient if the callback function has a C interface, in which case you also need a free extern "C" wrapper function. *)正如@James指出的那样,如果回调函数有一个C接口,这还不够,在这种情况下你还需要一个免费的extern "C"包装函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM