fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. class Method
  6. {
  7. T* object;
  8. void (T::*method)();
  9. public:
  10. Method(T* o, void (T::*m)()) : object(o), method(m) {}
  11. void operator()() { (object->*method)(); }
  12. };
  13.  
  14. class Foo
  15. {
  16. public:
  17. void a() { cout << "a" << endl; }
  18. void b() { cout << "b" << endl; }
  19. };
  20.  
  21. int main() {
  22. Foo foo;
  23. Method<Foo> method_a(&foo, &Foo::a);
  24. Method<Foo> method_b(&foo, &Foo::b);
  25.  
  26. method_a();
  27. method_b();
  28. // your code goes here
  29. return 0;
  30. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
a
b