根据变量调用函数?

时间:2016-09-11 11:45:53

标签: c++

你可以根据整数是一个函数来调用函数吗?

这就是我的意思:

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

我知道这不行,但我希望你明白这一点。

有没有办法做这样的事情?

5 个答案:

答案 0 :(得分:18)

是的,有办法做到这一点。

//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,可以使用function pointers

答案 1 :(得分:13)

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

答案 2 :(得分:5)

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

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

答案 3 :(得分:3)

支持字符串输入:

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;

答案 4 :(得分:2)

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

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

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