我们为客户提供了一个简单的通信库。
我的问题是:如何保存我们客户类中方法的指针?
感谢您的任何帮助!
我的问题是:如何保存我们客户类中方法的指针?
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);
};
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 i want to call the method from the customer
customer.o->customer.f(); //<- ???
}
customer.cpp:
class AnyCustomerClass {
private:
Library lib;
public:
AnyCustomerClass() {
//< here the customer sets his method which I should call
lib.SetCallback(this, &AnyCustomerClass::callback());
}
callback() {
// do something
}
}
感谢您的任何帮助!