使用using 定义函数指针及回调

示例:

class GMXcanvas : public CStatic
{
// Construction
public:
  GMXcanvas();

// Operations
public:

  using MsgNotifyFn = void(*)(GmxCanvasMsgEn msgType, void *userData);

  //typedef void (*MsgNotifyFn)(GmxCanvasMsgEn msgType, void *userData);

  void RegisterCallback(MsgNotifyFn fn,void *userData){ _msgCallback=fn;_userMsgCallbackData=userData;}

private:

  MsgNotifyFn _msgCallback;

  void *_userMsgCallbackData;

};

GMXcanvas::sumbit()

{

       ...

  if(_msgCallback){

    (*_msgCallback)(1200,_userMsgCallbackData);

  }  

       ...

}

 

class GMXgraph{

...

void OnCanvasMesage(GmxCanvasMsgEn msgType);

...

};

 

GMXgraph::GMXgraph(){

       ...

  auto fn_callback=[](GmxCanvasMsgEn msgType,void *userData)->void{

           GMXgraph *dlg = (GMXgraph*) userData;

           dlg->OnCanvasMessage(msgType);

  };

      canvas-> RegisterCallback(fn_callback,this);

      ...

}

 

GMXgraph::OnCanvasMesage(GmxCanvasMsgEn msgType)

{

    //do real things according to msgType

    ...

}

posted @ 2019-04-07 08:49  Reboost  阅读(2702)  评论(0编辑  收藏  举报