一.函数指针
#include <iostream>
typedef void (*callback)(int ,int);
class MyTest
{
public:
void setCallback(callback cb)
{
m_callback = cb;
}
void add(int a, int b)
{
m_callback(a, b);
}
private:
callback m_callback;
};
void onCallback(int a, int b)
{
std::cout << "a + b = " << a + b << std::endl;
}
int main(int argc, char *argv[])
{
MyTest test;
test.setCallback(onCallback);
test.add(1, 1);
return 0;
}
二.std::bind和std::function
#include <functional>
#include <iostream>
typedef std::function<void (int ,int)> callback;
class MyTest
{
public:
void setCallback(callback cb)
{
m_callback = cb;
}
void add(int a, int b)
{
m_callback(a, b