TPRammus
TPRammus

Reputation: 511

Calling a function depending on a variable?

Can you call a function depending on which number an integer is?

Here's 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 doesn't work but I hope you get the idea.

So is there a way to do something like that?

Upvotes: 11

Views: 6325

Answers (5)

Rakete1111
Rakete1111

Reputation: 48938

Yes, there is a way to do that.

//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 as a normal array:

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

Do note that the functions all have to have the same signature.


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

Upvotes: 19

barak manos
barak manos

Reputation: 30136

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

Here is a very basic scheme:

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

Upvotes: 1

xinaiz
xinaiz

Reputation: 7788

Support for string input:

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? Prepare for 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;

Upvotes: 3

Raindrop7
Raindrop7

Reputation: 3911

use pointer to function is a good thing:

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

Upvotes: 5

Pete Becker
Pete Becker

Reputation: 76245

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

Upvotes: 13

Related Questions