fork download
  1. #include <iostream>
  2.  
  3. void f1() { std::cout << "do f1\n"; }
  4. void f2() { std::cout << "do f2\n"; }
  5. void f3() { std::cout << "do f3\n"; }
  6.  
  7. using func_t = void (*)();
  8. using func_p = func_t*;
  9.  
  10. int main()
  11. {
  12. const int amountOfFunctions = 3;
  13.  
  14. std::cout << "static\n";
  15. //void (*functions_1[amountOfFunctions])() = { f1, f2, f3 };
  16. func_t functions_1[] = { f1, f2, f3 };
  17. for (size_t i = 0; i < amountOfFunctions; ++i) {
  18. functions_1[i]();
  19. }
  20.  
  21. std::cout << "\ndynamic\n";
  22. //func_t* functions_2 = new func_t[amountOfFunctions]{f1, f2, f3};
  23. void(**functions_2)() = new (void(*[amountOfFunctions])()){ f1, f2, f3 };
  24. for (size_t i = 0; i < amountOfFunctions; ++i) {
  25. functions_2[i]();
  26. }
  27. delete[] functions_2;
  28.  
  29. std::cout << "\n\n press [Enter]...";
  30. std::cin.get();
  31. }
  32.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
static
do f1
do f2
do f3

dynamic
do f1
do f2
do f3


   press [Enter]...