我有一个函数指针定义:
typedef void (*EventFunction)(int nEvent);
Run Code Online (Sandbox Code Playgroud)
有没有办法用特定的C++对象实例处理该函数?
class A
{
private:
EventFunction handler;
public:
void SetEvent(EventFunction func) { handler = func; }
void EventOne() { handler(1); }
};
class B
{
private:
A a;
public:
B() { a.SetEvent(EventFromA); } // What do I do here?
void EventFromA(int nEvent) { // do stuff }
};
Run Code Online (Sandbox Code Playgroud)
编辑: Orion指出了Boost提供的选项,例如:
boost::function<int (int)> f;
X x;
f = std::bind1st(
std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
Run Code Online (Sandbox Code Playgroud)
不幸的是Boost对我来说不是一个选择.是否有某种"currying"函数可以用C++编写,它会将指向成员函数的指针包装到普通函数指针中?
io_iterator_t enumerator;
kern_return_t result;
result = IOServiceAddMatchingNotification(
mNotifyPort,
kIOMatchedNotification,
IOServiceMatching( "IOFireWireLocalNode" ),
serviceMatchingCallback,
(void *)0x1234,
& enumerator );
serviceMatchingCallback((void *)0x1234, enumerator);
如果我将serviceMatchinCallback声明为静态然后它可以工作,但我不希望它是静态的.有没有办法将它传递给非静态回调函数?
谢谢