fork download
  1. #include<iostream>
  2. #include<functional>
  3.  
  4. class Foo{
  5. public:
  6. Foo(void* object): obj(object) {}
  7.  
  8. template<typename T>
  9. void callFunc(void (T::*func)()){
  10. m_func = std::bind(func, static_cast<T*>(obj));
  11. m_func();
  12. }
  13.  
  14. private:
  15. void* obj;
  16. std::function<void()> m_func;
  17. };
  18.  
  19. class Bar{
  20. public:
  21. Bar(): foo(this) {}
  22.  
  23. void callSomeFunc(){
  24. foo.callFunc(&Bar::someFunc);
  25. }
  26.  
  27. void someFunc(){
  28. std::cout << "hi\n";
  29. }
  30.  
  31. private:
  32. Foo foo;
  33. };
  34.  
  35. int main(){
  36. Bar bar;
  37. bar.callSomeFunc();
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
hi