| View previous topic :: View next topic |
| Author |
Message |
Gary Wessle Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: string convert to function name |
|
|
Hi
is there a way to convert a string to a function name and fire it. like
void his_fun(){
cout << "his is here" << endl;
}
vector<string> vec;
vec.push_back("his");
vec.push_back("me");
for(i=0; i<vec.size(); i++)
string var = vec[i] + _fun;
fire(var); and it will fire the routine "his_fun?
is so, how?
thanks |
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: Re: string convert to function name |
|
|
Gary Wessle wrote:
| Quote: | Hi
is there a way to convert a string to a function name and fire it.
|
No. |
|
| Back to top |
|
 |
benben Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: Re: string convert to function name |
|
|
Gary Wessle wrote:
| Quote: | Hi
is there a way to convert a string to a function name and fire it. like
void his_fun(){
cout << "his is here" << endl;
}
vector<string> vec;
vec.push_back("his");
vec.push_back("me");
for(i=0; i<vec.size(); i++)
string var = vec[i] + _fun;
fire(var); and it will fire the routine "his_fun?
is so, how?
|
#include <string>
#include <map>
#include <iostream>
typedef void (*func_ptr)(void);
std::map<std::string, func_ptr> functions;
void fun1()
{
std::cout << "fun1\n";
}
void fun2()
{
std::cout << "fun2\n";
}
int main()
{
functions["fun1"] = &fun1;
functions["fun2"] = &fun2;
std::string name;
std::cin >> name;
functions[name](); // invoke
}
Ben |
|
| Back to top |
|
 |
Osamede.Zhang Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: Re: string convert to function name |
|
|
| It's interesting. |
|
| Back to top |
|
 |
|