fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct IFoo{
  5. virtual void doWork() = 0;
  6. };
  7.  
  8. template<class T>
  9. struct MemberFuncFoo : public IFoo{
  10. typedef void (T::*FuncType)();
  11.  
  12. MemberFuncFoo(T *instance, FuncType memberFunc):
  13. m_instance(instance),
  14. m_memberFunc(memberFunc)
  15. {}
  16.  
  17. virtual void doWork() override{
  18. (m_instance->*m_memberFunc)();
  19. }
  20.  
  21. private:
  22. T* m_instance;
  23. FuncType m_memberFunc;
  24. };
  25.  
  26. struct Bar{
  27. void baz(){
  28. cout << "baz" << endl;
  29. }
  30. };
  31.  
  32. int main() {
  33. Bar bar;
  34. IFoo *ptr = new MemberFuncFoo<Bar>(&bar,&Bar::baz);
  35. ptr->doWork();
  36. // your code goes here
  37. return 0;
  38. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
baz