根据变量调用函数

Calling a function depending on a variable?

本文关键字:函数 调用 变量      更新时间:2023-10-16

是否可以根据整数来调用函数?

我的意思是:

#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!";
}

我知道这行不通,但我希望你能明白。

有没有办法做到这一点呢?

是的,有一种方法可以做到。

//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;

那么你可以把它当作一个普通的数组来使用:

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

请注意,所有函数必须具有相同的签名。


如果出于某种原因不想使用std::function,可以使用函数指针

switch(whichFunction) {
    case 1: function1(); break;
    case 2: function2(); break;
    case 3: function3(); break;
}

使用指针指向函数是件好事:

#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;
}

支持字符串输入:

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
}

写得太长?准备迎接风暴!

#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)

和使用:

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

您可以使用多态性概念(通过虚函数和继承)。

这是一个非常基本的方案:

#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;
}