相关疑难解决方法(0)

在特定对象实例上调用C++函数指针

我有一个函数指针定义:

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++编写,它会将指向成员函数的指针包装到普通函数指针中?

c++ pointers function

13
推荐指数
4
解决办法
3万
查看次数

如何将非静态成员函数作为回调传递?

 
io_iterator_t enumerator;
kern_return_t   result;
result = IOServiceAddMatchingNotification(
             mNotifyPort,
             kIOMatchedNotification,
             IOServiceMatching( "IOFireWireLocalNode" ),
             serviceMatchingCallback, 
             (void *)0x1234,
             & enumerator );

serviceMatchingCallback((void *)0x1234, enumerator);

如果我将serviceMatchinCallback声明为静态然后它可以工作,但我不希望它是静态的.有没有办法将它传递给非静态回调函数?

谢谢

c++ function callback member non-static

7
推荐指数
2
解决办法
7703
查看次数

标签 统计

c++ ×2

function ×2

callback ×1

member ×1

non-static ×1

pointers ×1