小编Sas*_*cha的帖子

用C++回调给类成员

我们为客户提供简单的通信库.

我的问题是:如何从我们客户的类中保存指向方法的指针?

Library.h 是头文件,包含客户建立通信所需的所有方法.

library.cpp是我们的代码.在某处我必须保存指向我们客户的回调函数方法的指针.

customer.cpp 是客户如何使用我们的库的一个例子.

library.h:

// This is the header file what our customer gets
class Library {
  public:
    template <class Object, class Function>
    void SetCallback(Object &obj, Function f);
};
Run Code Online (Sandbox Code Playgroud)

library.cpp:

struct T_CUSTOMER {
    Object o;   // <- ???
    Function f; // <- ???
} customer;

void Library::SetCallback(Object &obj, Function f) {
    //Saving the method from our costumer
    customer.o = obj;   // <- ???
    customer.f = f;     // <- ???
}

void someFunction(void) {
    // here …
Run Code Online (Sandbox Code Playgroud)

c++ methods function callback

6
推荐指数
2
解决办法
7237
查看次数

标签 统计

c++ ×1

callback ×1

function ×1

methods ×1