fork(1) download
  1. #include <iostream>
  2.  
  3. struct fooA {
  4. int foo1(int x, int y) { return x + y; }
  5. int foo2(int x, double y, void*) { return x + y; }
  6. };
  7. struct fooB {
  8. void foo1(int x, int y) { std::cout << "fooB(" << x << ", " << y << ")\n"; }
  9. void foo2(int x, double y, void* z) { std::cout << "fooB(" << x << ", " << y << ", " << z << ")\n"; }
  10. };
  11. struct fooC {
  12. void foo1(int x, int y) { std::cout << "fooC(" << x << ", " << y << ")\n"; }
  13. void foo2(int x, double y, void* z) { std::cout << "fooC(" << x << ", " << y << ", " << z << ")\n"; }
  14. };
  15.  
  16. class foo {
  17. public:
  18. void foo1(int x, int y)
  19. {
  20. dispatch(&fooA::foo1, &fooB::foo1, &fooC::foo1, x, y);
  21. }
  22. void foo2(int x, double y, void *z)
  23. {
  24. dispatch(&fooA::foo2, &fooB::foo2, &fooC::foo2, x, y, z);
  25. }
  26.  
  27. private:
  28. template <typename... Args>
  29. void dispatch(int (fooA::* f)(Args...),
  30. void (fooB::* g)(Args...),
  31. void (fooC::*h)(Args...),
  32. Args... args)
  33. {
  34. switch((m_Member.*f)(args...)) {
  35. case 1: (m_Member1.*g)(args...); return;
  36. case 2: (m_Member2.*h)(args...); return;
  37. }
  38. }
  39.  
  40. fooA m_Member;
  41. fooB m_Member1;
  42. fooC m_Member2;
  43. };
  44.  
  45. int main() {
  46. foo dispatcher;
  47. dispatcher.foo1(1, 0);
  48. dispatcher.foo2(1, 1, 0);
  49. return 0;
  50. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
fooB(1, 0)
fooC(1, 1, 0)