I\'ve managed to write a template class to work like a callback, learned from the accepted answer of this question How to define a general member function pointer.
I wis
There's no need for inheritance here. Just use std::function to store the member function pointers and std::bind to bind together the member function pointer and the object instance.
std::function
std::bind
#include #include #include #include struct Apple { void Red () { std::cout << "aa\n"; } }; struct Orange { void Blue () { std::cout << "bb\n"; } }; int main() { std::map> m; Apple a; Orange o; m["apple"] = std::bind(&Apple::Red, a); m["orange"] = std::bind(&Orange::Blue, o); m["apple"](); m["orange"](); }
Output:
aa bb