I'm trying to create something like MsgProc
in Win32. When they declare the MsgProc
function they have the CALLBACK
type before it. So, all I'm trying to is create my own messsage function that calls my function to process the messages. It is basically the same thing when messages ge开发者_如何学Ctting 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.
Here's an example of passing a lambda to a timer function.
#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:
http://www.newty.de/fpt/fpt.html
If you can use Boost, I would definitely give Boost.Signals a shot, they provide the functionality you are looking for in a clean and safe way (Boost.Signals2 even in thread-safe way.)
精彩评论