简体   繁体   English

c ++ CALLBACK函数类型

[英]c++ CALLBACK function type

I'm trying to create something like MsgProc in Win32. 我正在尝试在Win32中创建类似MsgProc东西。 When they declare the MsgProc function they have the CALLBACK type before it. 当他们声明MsgProc函数时,它们之前有CALLBACK类型。 So, all I'm trying to is create my own messsage function that calls my function to process the messages. 所以,我正在尝试创建自己的messsage函数,调用我的函数来处理消息。 It is basically the same thing when messages getting sent and processed. 消息发送和处理时基本相同。 My question is how can i create the Same process?. 我的问题是如何创建相同的流程? An example would be great. 一个例子就是很棒。

Other than the classics function pointers and function objects you may be interested by the new C++0x lambdas. 除了经典函数指针和函数对象之外,您可能会对新的C ++ 0x lambdas感兴趣。

Here's an example of passing a lambda to a timer function. 这是将lambda传递给计时器函数的示例。

#include <windows.h>
#include <iostream>
#include <functional>

void onInterval(DWORD interval, std::function<void ()> callback) {
  for (;;) {
    Sleep(interval);
    callback();
  }
}

int main() {
  onInterval(1000, []() {std::cout<<"Tick! ";});
}

If you're in C++, just use a function pointer in your class: 如果您使用的是C ++,只需在类中使用函数指针:

http://www.newty.de/fpt/fpt.html http://www.newty.de/fpt/fpt.html

如果你可以使用Boost,我肯定会给Boost.Signals一个机会,它们以干净和安全的方式提供你正在寻找的功能( Boost.Signals2甚至以线程安全的方式。)

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

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