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.
// 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(&openLiveViewWindow);
...
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:
error C2276: '&' : illegal operation on bound member function expression
Some research on this pointed out to std::function()
and std::bind()
but I'm not familiar with these and would appreciate some pointers on how to resolve this.
RESOLUTION: For anyone interested, here are the bricks of the final solution
// TimedButton.h
#include <functional>
// define timer callback prototype
typedef std::function<void()> 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 <functional>
...
_buttonSettings.setCallbackShortTime(std::bind(&CMyDlg::openLiveViewWindow, this));
_buttonSettings.setCallbackLongTime(std::bind(&CMyDlg::openAdminPanelWindow, this));
std::function
is a polymorphic function object, which can wrap up any type of callable object with a particular signature. In your case, you want it to take no arguments and return no value, so you can define:std::bind
allows you to adapt a callable object to one of a different signature, by binding arguments to parameters. In your case, you want to adapt a member function by binding it to a particular object to invoke it on:Note that these were introduced in 2011, so older compilers won't support them. In that case, you could either use the very similar Boost or TR1 libraries, or make your own callable class containing a pointer-to-member-function and a pointer/reference to the object you want to invoke it on.
The old-fashioned way to do this is to make your callback function accept an additional
void*
parameter, for which you pass a pointer to the object you wish to call the function on. Then you use a static member function for the callback and let it cast the pointer to the proper object and call your true callback.You could create a polymorphic template class that acts as a function pointer.
To initialize a functor object:
And to use it:
Here's an example:
Here's a great resource about functors along with the implementation I've described above. http://www.newty.de/fpt/functor.html#chapter4
You can not pass a pointer to a method of a class, only plain functions. I suggest digging into
std::function()
, since you are using VS2010, which supports them. There is a nice (and long) tutorial describing them and more here.