What elegant method callback method should I use?

I am surprised that this question has not been asked before on SO (well, at least I could not find it).

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

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

All suggestions are welcome!

+4
source share
3 answers

boost :: function for a single callback, boost :: signal or boost :: signals2 , when you can register more than one callback, using boost :: bind to bind member methods (or adapt signatures in different ways).

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

+9
source

No boost::function (combined with boost::bind ) elegant enough? It will also take you away from the unpleasant (but standard compatible) implementation details, such as member pointers greater than void* , which was a problem in the callback library for the old Windows CE system. I would rather use a well-known library than solve these problems myself.

+2
source

You can find a good introduction to callbacks with C ++ here . I used this article as a basis for implementation when boost was not an option.

+2
source

Source: https://habr.com/ru/post/1308788/


All Articles