超简单回调实现

回调的本质就是利用函数指针,让客户程序员定义事件发生时的处理过程。

#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();

   return 0 ;

}


原文地址:https://www.cnblogs.com/mumuliang/p/1883908.html