views:

57

answers:

2

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.

+1  A: 

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

John Knoeller
That's the answer I was looking for, thanks.
A: 

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++...

Kosta