Function call depending on a variable?

Is it possible to call a function depending on which number is an integer?

Here is what I mean:

#include <iostream> using namespace std; int whichFunction; int main() { cout << "Which function do you want to call?"; cin >> whichFunction; function[whichFunction](); //If you entered 1, it would call function1 - same with 2, 3 //or Hihihi (of course only when whichFunction would be a string) } void function1() { cout << "Function No. 1 was called!"; } void function2() { cout << "Function No. 2 was called!"; } void functionHihihi() { cout << "Function Hihihi was called!"; } 

I know this does not work, but I hope you understand this idea.

So is there a way to do something like this?

+8
c ++
source share
5 answers

Yes, there is a way to do this.

 //Array for the functions std::array<std::function<void()>, 3> functions = { &function1, &function2, &function3 }; //You could also just set them manually functions[0] = &function1; functions[1] = &function2; functions[2] = &function3; 

Then you can use it like a regular array:

 functions[whichFunction](); //Calls function number 'whichFunction' 

Please note that all functions must have the same signature.


If you do not want to use std::function for any reason, you can use function pointers .

+18
source share
 switch(whichFunction) { case 1: function1(); break; case 2: function2(); break; case 3: function3(); break; } 
+13
source share

Using a function pointer is good:

 #include <iostream> #include <string> using namespace std; void foo1(){cout << "Foo1 says hello!" << endl;} void foo2(){cout << "Foo2 says hello!" << endl;} void foo3(){cout << "Foo3 says hello!" << endl;} void foo4(){cout << "Foo4 says hello!" << endl;} int main() { system("color 1f"); int input; cout << "Which function you wanna call: "; cin >> input; cout << endl; void (*pFunc)() = NULL; switch(input) { case 1: pFunc = foo1; break; case 2: pFunc = foo2; break; case 3: pFunc = foo3; break; default: cout << "No function available!" << endl; } if(NULL != pFunc) //avoiding usage of a NULL ptr (*pFunc)(); cout << endl << endl << endl; return 0; } 
+5
source share

Line input support:

 std::map<std::string, std::function<void()>> le_mapo; le_mapo["1"] = &function1; le_mapo["2"] = &function2; le_mapo["3"] = &function3; le_mapo["Hihihi"] = &functionHihihi; std::string input; std::cin >> input; auto check = le_mapo.find(input); if (check != le_mapo.end()) { le_mapo[input](); //call function } else { //not found } 

Too long to write? Get ready for the storm!

 #define BE_EVIL(map, function, x) map[ #x ] = & function ## x //custom macro with predefined function name (here: just function) #define BE_EVIL_FUN(map, x) BE_EVIL(map, function, x) //same, with predefined map name #define BE_EVIL_JUST_X(x) BE_EVIL_FUN(le_mapo, x) 

And use:

 //"connect" string to function BE_EVIL(le_mapo, function, 1); //le_mapo[ "1" ] = & function1; 
+3
source share

You can use the concept of polymorphism (through virtual functions and inheritance).

Here is a very simple diagram:

 #include <iostream> #include <vector> using namespace std; class Obj { public: virtual void function() = 0; }; class Obj1 : public Obj { public: virtual void function() {cout << "Function No. 1 was called!";} }; class Obj2 : public Obj { public: virtual void function() {cout << "Function No. 2 was called!";} }; class Obj3 : public Obj { public: virtual void function() {cout << "Function No. 3 was called!";} }; int main() { vector<Obj*> objects; objects.push_back(new Obj1); objects.push_back(new Obj2); objects.push_back(new Obj3); cout << "Which function do you want to call?"; int whichFunction; cin >> whichFunction; if (1 <= whichFunction && whichFunction <= objects.size()) objects[whichFunction-1]->function(); for (auto object : objects) delete object; return 0; } 
+2
source share

All Articles