replace typedef void (*callback)();withtypedef boost::function<void()> callback;
A linked function does not create a regular function, so you cannot just store it in a regular function pointer. However, it boost::functioncan handle anything if it is allowed with the correct signature, so you want to. It will work with a function pointer or functor created with a binding.
:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
class A {
public:
void print(const std::string &s) {
std::cout << s << std::endl;
}
};
typedef boost::function<void()> callback;
class B {
public:
void set_callback(callback cb) {
m_cb = cb;
}
void do_callback() {
m_cb();
}
private:
callback m_cb;
};
void regular_function() {
std::cout << "regular!" << std::endl;
}
int main() {
A a;
B b;
std::string s("message");
b.set_callback(boost::bind(&A::print, &a, s));
b.do_callback();
b.set_callback(regular_function);
b.do_callback();
}