I have a void function inside of a class. In old C++ i’d make a function static taking the class name as a parameter and had my own class which took a static void function + a void* for me to easily call it.
However that feels old school. It also isn’t templated which feels like i could be doing more. What is a more modern way of creating callbacks to myclassVar.voidReturnVoidParamFunc
Use
std::functionand lambdas (orstd::bind()) to store callables:Result:
Compiles and run: http://ideone.com/T6wVp
std::functioncan be used as any copyiable object, so feel free to store it somewhere as a callback, like in object’s member. It also means that you can freely put it in standard containers, likestd::vector< std::function< void () > >.Also note that equivalent boost::function and boost::bind have been available for years.