A: 

No. Non-static methods need an object to operate on. If you were to merely pass the method you would also need some way to tell the function which object to call the method on.

Kyle Lutz
A: 

No, a non-static member would expect an object, and the caller (call-backer) does not have and will not provide one.

Erik Elmgren
A: 

A non-static function has an implicit this parameter, and thus would have the wrong signature for the callback.

Sorry, no easy way to avoid the jump island.

Mike DeSimone
but then the member variables that i am using inside the static member method serviceMatchingCallback(...) should also be static. And static member vars have to be declared at the top, which i do not want.correct me if i am wrong.
rashid
I'm having trouble parsing your reply, since members can only be declared in a class declaration. I also don't know what you mean by "static member vars have to be declared at the top". (Top of what? Class? Source file?) At this point, I'll simply point you to R Samuel Klatchko's more complete answer above.
Mike DeSimone
A: 

if you put this line in your constructor (or in any instance method) then you should be able to do this.instanceMethod() to refer to the instance method.

Abby Fichtner
+4  A: 

You could keep it static, but use the userdata to store the this pointer in addition to whatever other userdata you want (by packing them into a structure, for example) and then call an object-specific callback from the static version by calling this->someCallback (where this is the pointer stored in the userdata, of course).

Sydius
+2  A: 

Not directly.

The non-static function pointer (known as a member function pointer) has a hidden 'this' parameter so the types don't match. The static function has no 'this' pointer.

To get around this, you need to be able to pass in a user data item which is the 'this' pointer of the object you want to use as a callback. Then, specify a static member that is passed the user data, converts it to a pointer to the class object and calls the non-static member on it.

Looking at the code you've posted, it's hard to tell if there is a user data object, possibly the last-but=one parameter.

Skizz
+4  A: 

The prototype for IOServiceMatchingCallback is not compatible with a non-static class method (and technically is not compatible with a static class method either), so you are not going to be able to use that.

But luckily IOServiceAddMatchingNotification supports a context pointer (or as they call it, a refCon) which will allow you to create a thunk that does not rely on global data.

You need to define a callback with compatible linkage (i.e. extern "C"). This function will cast your refCon to your object pointer and then forward the call to your instance method:

extern "C"
void io_callback(void *refcon, io_iterator_t iterator)
{
    myclass *c = static_cast<myclass *>(refcon);
    c->real_callback(iterator);
}

Then, when you call IOServiceAddMatchingNotification, make sure to pass a pointer to your object for refCon (here I'm assuming you call IOServiceAddMatchingNotification from a member function and you have a this pointer):

result = IOServiceAddMatchingNotification(
             mNotifyPort,
             kIOMatchedNotification,
             IOServiceMatching( "IOFireWireLocalNode" ),
             serviceMatchingCallback, 
             this,
             &enumerator );
R Samuel Klatchko
+1 See also: http://stackoverflow.com/questions/2068022/in-c-is-it-safe-portable-to-use-static-member-function-pointer-for-c-api-callb
Martin York
A: 

EDIT I just noticed you are using the user space IOKit API, not the kext side, which makes this post irrelevant.


Assuming you are working within the OS X kernel, you actually can do this. You can use the OSMemberFunctionCast macro to convert a member function pointer to a plain C function pointer, do note that it should be called with the first argument pointing to an instance of the class, eg.

IOServiceMatchingCallback mycb = OSMemberFunctionCast(IOServiceMatchingCallback,
    &myclassinstance, &MyClass::cb_method);

result = IOServiceAddMatchingNotification(
             mNotifyPort,
             kIOMatchedNotification,
             IOServiceMatching( "IOFireWireLocalNode" ),
             mycb,
             &myclassinstance,
             &enumerator);
Hasturkun