string convert to function name

G

Gary Wessle

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 + _fun;
fire(var); and it will fire the routine "his_fun?

is so, how?

thanks
 
B

benben

Gary said:
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 + _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
 
M

missdeer

Yeah, sereral C++ wrappers for some script languages are implemented in this
way.

benben said:
Gary said:
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 + _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
 
M

mlimber

benben said:
Gary said:
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 + _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
}


This is exceedingly dangerous. The [] lookup operation is non-const
because it does an insert cum default intialization if the key is not
found, and since the result (the function pointer) is immediately
dereferenced and invoked, this would mean dereferencing a null pointer,
which, as you know, is bad bad bad. The safe alternative is to use
map::find for lookups (unless you're absolutely sure that the string is
one of your keys, but even then, I'd prefer to err on the side of
caution to prevent future changes from creating problems).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top