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

 

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

}


 

相关文章:

  • 2022-12-23
  • 2021-06-03
  • 2021-06-29
  • 2021-12-10
  • 2021-10-19
  • 2021-12-30
  • 2021-12-12
猜你喜欢
  • 2021-10-18
  • 2021-07-22
  • 2021-12-28
  • 2022-12-23
  • 2022-03-04
  • 2021-07-29
  • 2022-12-23
相关资源
相似解决方案