473,800 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stringve c;
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 6615
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<stringve c;
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)(voi d);

std::map<std::s tring, func_ptrfunctio ns;

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" <benhonghatgmai ldotcom@nospam>
??????:45****** *************** **@news.optusne t.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<stringv ec;
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)(voi d);

std::map<std::s tring, func_ptrfunctio ns;

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<stringve c;
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)(voi d);

std::map<std::s tring, func_ptrfunctio ns;

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
3699
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 discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
2
2762
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 in a querystring, I can't figure out how to convert it back to a CipherMessage.
9
1847
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
3268
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 contains only 1 character, a '.' must follow the char. -If we find a character that isn't a letter (.&*-), we must swap it for a '/' and add the correct spaces. I tried making a function but my switch seems to have an error. If anyone
5
37342
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 As StringBuilder = New StringBuilder For i As Integer = 0 To arrBytes.Length - 1
5
12722
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
7
2632
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 subtle problems, such as the fact that a text file may wrap lines. It will be appearing on my website as soon as I get update access, assuming no one finds anything wrong with it. /*
28
301
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 suggested ("comp.lang.c++") just in case. I think the problem is that in my C++ class, we were taught to use cin, cout etc. but now I need to use the primitive C operations printf etc. and I don't understand exactly how they work.
14
2540
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 appreciated. Thank you!
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10250
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.