回调的本质就是利用函数指针,让客户程序员定义事件发生时的处理过程。
#include <iostream>
typedef void (*cbFunc)(void); // type the pointer of callback function ,no return,no params
class A
{
cbFunc eventhandle;
public:
A(){};
~A(){};
void AddEventHandler(cbFunc func){eventhandle = func;};
void OnEvent(){eventhandle();};
};
void clientEventHandle()
{
std::cout << "Do whatever" << std::endl;
return;
}
// argc: the number of argv
// argv: an array of arguments,
// args[0] always is the path and name of the program itself.
int main(int argc, char** argv)
{
A a;
a.AddEventHandler(clientEventHandle);
a.OnEvent();
typedef void (*cbFunc)(void); // type the pointer of callback function ,no return,no params
class A
{
cbFunc eventhandle;
public:
A(){};
~A(){};
void AddEventHandler(cbFunc func){eventhandle = func;};
void OnEvent(){eventhandle();};
};
void clientEventHandle()
{
std::cout << "Do whatever" << std::endl;
return;
}
// argc: the number of argv
// argv: an array of arguments,
// args[0] always is the path and name of the program itself.
int main(int argc, char** argv)
{
A a;
a.AddEventHandler(clientEventHandle);
a.OnEvent();
return 0 ;
}