Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What elegant method callback design should be used?

I'm surprised this question wasn't asked before on SO (well, at least I couldn't find it).

Have you ever designed a method-callback pattern (something like a "pointer" to a class method) in C++ and, if so, how did you do it ?

I know a method is just a regular function with some hidden this parameter to serve as a context and I have a pretty simple design in mind. However, since things are often more complex than they seem to, I wonder how our C++ gurus would implement this, preferably in an elegant and standard way.

All suggestions are welcome !

like image 908
ereOn Avatar asked Dec 13 '22 22:12

ereOn


2 Answers

boost::function for single callback, boost::signal or boost::signals2 when more than one callbacks can be registered, using boost::bind to bind member methods (or adapting the signatures in different ways).

If you have access to a compiler with C++0x/C++11 support it may have std::function and std::bind that are the new standard version of boost::function and boost::bind

like image 169
David Rodríguez - dribeas Avatar answered Dec 27 '22 02:12

David Rodríguez - dribeas


Isn't boost::function (in conjunction with boost::bind) elegant enough? This will also keep you away from nasty (yet standard-conforming) implementation details like pointers to members being larger than a void*, which was a problem in a callback library for an older Windows CE system. I'd rather use a well-known library than having to deal with these problems myself.

like image 29
OregonGhost Avatar answered Dec 27 '22 03:12

OregonGhost