•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,223 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,289 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 481 | Replies: 2
![]() |
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <map> #include <string> std::string foo() { return "Foo"; } std::string bar() { return "Bar"; } int main() { std::map<std::string, std::string (*)()> m; // Map the functions to the names m["foo"] = &foo; m["bar"] = &bar; // Display all of the mapped functions std::map<std::string, std::string (*)()>::const_iterator it = m.begin(); std::map<std::string, std::string (*)()>::const_iterator end = m.end(); while ( it != end ) { std::cout<< it->first <<"\t\"" << (it->second)() <<"\"\n"; ++it; } }
Last edited by Narue : Jul 17th, 2008 at 9:05 am.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Fire point-blank:
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <map> using namespace std; namespace // for example only { void DoOne() { cout << "One" << endl; } void DoTwo() { cout << "Two" << endl; } void DoNil() { cout << "Nil" << endl; } } void Mapper() { typedef void (*DoIt)(); typedef map<string,DoIt> MapCall; MapCall doit; doit.insert(MapCall::value_type("One",DoOne)); doit.insert(MapCall::value_type("Two",DoTwo)); doit.insert(MapCall::value_type("Nil",DoOne)); MapCall::const_iterator call; call = doit.find("One"); if (call != doit.end()) (*call).second(); else cout << "Unknown call requested" << endl; }
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How do I write code for PAC MAN! (C++)
- A multilingual coded Hello World! thread (Legacy and Other Languages)
- C++ Books (C++)
- Visual C++ Help Please (Game Development)
- Several C++ Designing Questions (C++)
- Website Help (JavaScript / DHTML / AJAX)
- grant access (C++)
- filestream && multidimensional arrays (C++)
- access Windows Address Book (C)
Other Threads in the C++ Forum
- Previous Thread: reading a file and storing it in a array
- Next Thread: C++ Game - Pseudo code



Linear Mode