Calling a function that was stored in an array

I have stored the name of some functions in an array, but now I need to use those functions. When the user chooses a function to access, how can I use the function name I saved to reach the intended function? Or can I?

Thank you in advance :-)
Last edited on
Could you show an example of what you have, say two functions and the array?
This is the basic skeleton:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string>;
#include <iostream>;

using namespace std;

void fn1()
{
	//do something
}

void fn2()
{
	//do something else
}

void main()
{
	int choice;
	int i = 0;

	string fn[2] = { "fn1()", "fn2()" };
	cout << "What function do you want to access?" << endl << endl;

	for (int choice = 1; choice <= 2; choice++)
	{
		cout << choice << ". " << fn[i] << endl;
		i++;
	}
	cin >> choice;
}


But how can I use that function name?
Last edited on
You can't, if you're trying to replicate an 'eval' that scripting languages like Ruby can do.

Create a map of your string keys to function pointers.
The simplest thing to do is to use a switch to call the function:
1
2
3
4
5
6
7
8
switch (choice) {
case 1:
    fn1();
    break;
case 2:
    fn2();
    break;
}


Alternatively, you could create a table of function names and function pointers, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

struct Func {
    std::string name;
    void (* fptr )();  // pointer to a function that takes no parameters and returns nothing.
};

void one()   { std::cout << "\nfunc one\n\n";   }
void two()   { std::cout << "\nfunc two\n\n";   }
void three() { std::cout << "\nfunc three\n\n"; }

int main() {
    Func funcs[] = {
        { "one",   one },
        { "two",   two },
        { "three", three },
        { "",      nullptr }   // a null function pointer marks the end of the table
    };

    while (true) {
        int n = 0;
        for ( ; funcs[n].fptr != nullptr; n++)
            std::cout << n + 1 << ". " << funcs[n].name << '\n';
        std::cout << n + 1 << ". quit\n";

        int choice = -1;
        while (true) {
            std::cout << "What function do you want: ";
            std::cin >> choice;
            if (choice >= 1 && choice <= n + 1)
                break;
            std::cout << "Enter a number from 1 to " << n + 1 << ".\n";
        }

        if (choice == n + 1)
            break;

        funcs[choice - 1].fptr();
    }
}

Last edited on
Alright. Thank you all!
Similar idea of same-signature method map, but feels like cleaner syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <map>
#include <functional>

using namespace std;

int Add(int a, int b)
{
    return a + b;
}

int Sub(int a, int b)
{
    return a - b;
}

int Mul(int a, int b)
{
    return a * b;
}

int Div(int a, int b)
{
    return a / b;
}

int main() 
{
    map<string, function<int(int,int)>> funcs
    {
        { "Add", Add },
        { "Sub", Sub },
        { "Mul", Mul },
        { "Div", Div },
    };

    for (auto& kv : funcs)
        cout << kv.first << " : " << kv.second(4,5) << endl;

    return 0;
}


Add : 9
Div : 0
Mul : 20
Sub : -1


While you might have trouble mixing in varying method signatures, your use case probably wouldn't need it.
Topic archived. No new replies allowed.