c++ function with callback code example
Example 1: c++ callback member function
class Foo {
int (*func)(int);
public:
void setCallback( int (*cFunc)(int) ) {
func = cFunc;
}
void set() {
setCallback(callback);
}
void use() {
func(5);
}
static int callback(int param) {
return 1;
}
};
Example 2: c++ callback function
class Base {
public:
void doSomething() {
using namespace std::placeholders;
something.setCallback(std::bind(&Base::callback, this, _1));
}
void callback(int i) {
std::cout << "Callback: " << i << std::endl;
}
}