Re: Function Pointer Query

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance





"Igor Tandetnik" wrote:

"Rich S." <RichS@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:CAAA1E12-6432-4B32-9053-8047799C13E0@xxxxxxxxxxxxx
Is it possible to have a function pointer that points to a function
in a class that is NON-static?

Here's a pointer to member function:

class C {
public:
void f(int);
};

typedef void (C::*PF)(int);
PF pf = &C::f;

C c;
(c.*pf)(1); // calls c.f(1)

Note that pointer to member function is incompatible with regular
function pointer. You can't pass the former where the latter is
expected. If you hope to pass a member function pointer to some API that
needs a callback, you are sadly out of luck. Most APIs taking a callback
require a non-member or a static member function.

Since static member functions are so inconvenient, I am hoping that
there is a way to pass a pointer to a function that is non-static.

Most APIs that take a callback also take a context (aka closure)
parameter that they pass right back to the callback. Let's take
EnumWindows:

class C {
public:
void EnumWindows();
BOOL EnumProc(HWND);
static BOOL CALLBACK EnumProcStatic(HWND, LPARAM);
};

void C::EnumWindows() {
::EnumWindows(EnumProcStatic, reinterpret_cast<LPARAM>(this));
}

BOOL CALLBACK C::EnumProcStatic(HWND hwnd, LPARAM lparam) {
C* pThis = reinterpret_cast<C*>(lparam);
return pThis->EnumProc(hwnd);
}

BOOL C::EnumProc(HWND hwnd) {
// Do actual work here
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925




Thank you Igor. I was vaguely aware of things called "pointer to member",
but I did not know how they fit into the scheme of things before your answer.

I guess I will have to implement one of the workarounds that you and Carl
mentioned.

Rich
.



Relevant Pages

  • Re: [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... cast to unsigned long and cast back in the callback. ... number - not a pointer cast to unsigned long, not an index in array, etc. ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: legality of signature changes of callback functions
    ... Suppose that some type of callback function is declared ... to take a pointer to some struct as one of its argument. ... taking a void* for this argument. ... converts it to a Something* to add type information. ...
    (comp.lang.c)
  • Re: Global restricted function pointer
    ... void; ... I _can_ declare a global restricted object pointer, ... And even if it did, you do *not* want it optimized into a register, ... I.E. each callback returns the next callback instead of ...
    (comp.lang.c)
  • Re: Custom filter
    ... You expect a function pointer so better be safe and tell the ... STDMETHODIMP CPrintPressFilter::Setcallback(void* Callback) ... public delegate void MyCallback; ...
    (microsoft.public.win32.programmer.directx.video)