C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string convert to function name

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gary Wessle
Guest





PostPosted: Thu Nov 09, 2006 10:11 am    Post subject: string convert to function name Reply with 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?

thanks
Back to top
Rolf Magnus
Guest





PostPosted: Thu Nov 09, 2006 10:11 am    Post subject: Re: string convert to function name Reply with quote



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





PostPosted: Thu Nov 09, 2006 10:11 am    Post subject: Re: string convert to function name Reply with quote



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





PostPosted: Thu Nov 09, 2006 10:11 am    Post subject: Re: string convert to function name Reply with quote

It's interesting.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.