>Is there a way that I can call a function by having its name stored in a variable?
Yes, you can do something like what you want with a function pointer:
Code:
#include <iostream>
typedef void (*fptr)();
static void function()
{
std::cout<<"This is the function\n";
}
static void function2()
{
std::cout<<"This is the other function\n";
}
int main()
{
void (*fp)() = function;
fp();
fp = function2;
fp();
return 0;
}
To match the code you gave, you will need an array of function pointers, like so:
Code:
#include <iostream>
void function()
{
std::cout<<"This is the function\n";
}
void function2()
{
std::cout<<"This is the other function\n";
}
int main()
{
void (*fp[])() = {
{ function },
{ function2 }
};
for ( int i = 0; i < 2; i++ )
fp[i]();
return 0;
}
-Prelude