开发者

Asynchronous Completion Routines I/O, Pointer to routine encapsulated in class

开发者 https://www.devze.com 2022-12-23 23:40 出处:网络
I was 开发者_如何学运维wondering if there was anyway to use functions like ReadFileEx that require a pointer to a function in a class WITHOUT marking the function as static? Thanks in advance. SBP.You

I was 开发者_如何学运维wondering if there was anyway to use functions like ReadFileEx that require a pointer to a function in a class WITHOUT marking the function as static? Thanks in advance. SBP.


You can always extend the OVERLAPPED struct that you pass to include a pointer to your object. Then, pass a function that calls a member function on that object. Somewhat like this:

typedef struct _MYOVERLAPPED
{
    OVERLAPPED ol;
    MyObject *obj;
} MYOVERLAPPED, *LPMYOVERLAPPED;

void ReadCompleted(DWORD err, DWORD read, LPMYOVERLAPPED overlap)
{
  overlap->obj->foo();
}

Sorry if there's some slight syntax errors in above code, it's been a while since I actually wrote some C++...


No, non-static class functions have an implied first argument (this) which is incompatible with their use as a callback for ReadFileEx etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号