fork download
  1. #include <iostream>
  2.  
  3. void Foo() { std::cout << "Foo" << std::endl; }
  4. void Bar() { std::cout << "Bar" << std::endl; }
  5. void FooBar(){ std::cout << "FooBar" << std::endl; }
  6. void Baz() { std::cout << "Baz" << std::endl; }
  7. void FooBaz(){ std::cout << "FooBaz" << std::endl; }
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13. void (*pFunc[])() = {Foo, Bar, FooBar, Baz, FooBaz};
  14.  
  15. int choice;
  16. std::cout << "Which function: ";
  17. std::cin >> choice;
  18. std::cout << std::endl;
  19.  
  20. pFunc[choice]();
  21.  
  22.  
  23. // or iteratley call them all:
  24.  
  25. std::cout << "calling functions iteraely:" << std::endl;
  26.  
  27. for(int i(0); i < 5; i++)
  28. {
  29. pFunc[i]();
  30. }
  31.  
  32. std::cout << std::endl;
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 3476KB
stdin
3
stdout
Which function: 
Baz
calling functions iteraely:
Foo
Bar
FooBar
Baz
FooBaz