The functions should be specified with their address with
&
.
typedef std::map<uint32_t, void(*)()> function;
function menu = {
{1, &tall },
{2, &call },
};
Call the function with parenthesis like this:
it->second()
The function can be called from menu directly like
menu[a]()
if you make sure
a
is either
1
or
2
, in other words, within the
menu
boundary.
#include <iostream>
#include <map>
void tall()
{
std::cout << "tall() called\n";
}
void call()
{
std::cout << "call() called\n";
}
typedef std::map<uint32_t, void(*)()> function;
function menu = {
{1, &tall },
{2, &call },
};
int main()
{
uint32_t a;
std::cout << "choose menu [1~2]" << std::endl;
std::cin >> a;
if(a >= 1 && a <= 2)
menu[a]();
else
{
std::cout << a << " menu not found" << std::endl;
}
return 0;
}