473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 6577
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
9
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
1
by: The_Kingpin | last post by:
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction: -The first letter of the first and last name must be uppercase. -If a first name...
5
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
28
by: pradeep | last post by:
Hello friends: I know some people here don't like to answer C++ questions, but I believe this is really about the underlying C code. Anyway I have posted as well to the other group someone...
14
by: Jamenson | last post by:
Hi everyone! I want to convert strings like "LEONARDO DI CAPRIO" to "Leonardo di Caprio". The function StrConv converts to "Leonardo Di Caprio" and we think it is innapropriated. Any help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.