title: how to pass C++ callbacks between unrelated classes?
body: <p>In a non-boost project, I have a class which uses a timer based on a certain user action (button pressed/released). I want this class generic, so it takes callbacks for user defined actions.</p>
  
  <pre><code>// TimerClass.h
  typedef void (*timerCallback)(void);
  ...
  Class TimerClass : public CButton {
  public:
      void setCallbackShortTime(timerCallback clickFn) { shortCallback = clickFn;} ;
      void setCallbackLongTime(timerCallback clickFn) { longCallback = clickFn;} ;
  private:
      timerCallback shortCallback, longCallback;
  }
  
  
  // CMyDlg.h
  class CMyDlg : public CDialog
  {
  public:
      void openLiveViewWindow();
      void openAdminPanelWindow();
      TimerClass _buttonSettings;
  }
  
  // CMyDlg.cpp
  ...
  _buttonSettings.setCallbackShortTime(&amp;openLiveViewWindow);
  ...
  </code></pre>
  
  <p>Now, from another class (DialogClass) I can use the TimerClass but I cannot pass function pointers to the callback functions.  These functions are not static. The compiler ends up complaining:</p>
  
  <pre><code>error C2276: '&amp;' : illegal operation on bound member function expression
  </code></pre>
  
  <p>Some research on this pointed out to <code>std::function()</code> and <code>std::bind()</code> but I'm not familiar with these and would appreciate some pointers on how to resolve this.</p>
  
  <p><strong>RESOLUTION</strong>: For anyone interested, here are the bricks of the final solution</p>
  
  <pre><code>// TimedButton.h
  #include &lt;functional&gt;
  // define timer callback prototype
  typedef std::function&lt;void()&gt; timerCallback;
  ...
  class TimedButton : public CButton
  {
  public:
      TimedButton();
      ...
      void setCallbackShortTime(timerCallback clickFn) { _shortCallback = clickFn;} ;
      void setCallbackLongTime(timerCallback clickFn) { _longCallback = clickFn;} ;
  private:
      timerCallback _shortCallback;
      timerCallback _longCallback;
  }
  
  // TimedButton.cpp
  ...
  (_longCallback)();  // call long duration fn
  ...
  (_shortCallback)();     // call short duration fn
  
  // in MyDlg.cpp
  #include &lt;functional&gt;
  ...
  _buttonSettings.setCallbackShortTime(std::bind(&amp;CMyDlg::openLiveViewWindow, this));
  _buttonSettings.setCallbackLongTime(std::bind(&amp;CMyDlg::openAdminPanelWindow, this));
  </code></pre>