469,898 Members | 2,201 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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<stringvec;
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
Nov 9 '06 #1
5 5849
Gary Wessle wrote:
Hi

is there a way to convert a string to a function name and fire it.
No.

Nov 9 '06 #2
Gary Wessle wrote:
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<stringvec;
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_ptrfunctions;

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
Nov 9 '06 #3

It's interesting.

Nov 9 '06 #4
Yeah, sereral C++ wrappers for some script languages are implemented in this
way.

"benben" <benhonghatgmaildotcom@nospam>
??????:45***********************@news.optusnet.com .au...
Gary Wessle wrote:
>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<stringvec;
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_ptrfunctions;

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

Nov 9 '06 #5
benben wrote:
Gary Wessle wrote:
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<stringvec;
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_ptrfunctions;

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

Nov 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
2 posts views Thread by Nathan | last post: by
9 posts views Thread by Ronald Fischer | last post: by
5 posts views Thread by Mika M | last post: by
5 posts views Thread by XML newbie: Urgent pls help! | last post: by
7 posts views Thread by Malcolm | last post: by
28 posts views Thread by pradeep | last post: by
14 posts views Thread by Jamenson | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.