User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2008
Posts: 9
Reputation: sangham is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sangham sangham is offline Offline
Newbie Poster

map function pointer

  #1  
Jul 17th, 2008
Hi friends,

Can you please help me with the syntax of how I will map a string to a function pointer using map stl?


Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: map function pointer

  #2  
Jul 17th, 2008
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. std::string foo() { return "Foo"; }
  6. std::string bar() { return "Bar"; }
  7.  
  8. int main()
  9. {
  10. std::map<std::string, std::string (*)()> m;
  11.  
  12. // Map the functions to the names
  13. m["foo"] = &foo;
  14. m["bar"] = &bar;
  15.  
  16. // Display all of the mapped functions
  17. std::map<std::string, std::string (*)()>::const_iterator it = m.begin();
  18. std::map<std::string, std::string (*)()>::const_iterator end = m.end();
  19.  
  20. while ( it != end ) {
  21. std::cout<< it->first <<"\t\""
  22. << (it->second)() <<"\"\n";
  23. ++it;
  24. }
  25. }
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.
Reply With Quote  
Join Date: Jul 2008
Posts: 624
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 94
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: map function pointer

  #3  
Jul 17th, 2008
Fire point-blank:
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5.  
  6. namespace // for example only
  7. {
  8. void DoOne() { cout << "One" << endl; }
  9.  
  10. void DoTwo() { cout << "Two" << endl; }
  11.  
  12. void DoNil() { cout << "Nil" << endl; }
  13. }
  14.  
  15. void Mapper()
  16. {
  17. typedef void (*DoIt)();
  18. typedef map<string,DoIt> MapCall;
  19.  
  20. MapCall doit;
  21.  
  22. doit.insert(MapCall::value_type("One",DoOne));
  23. doit.insert(MapCall::value_type("Two",DoTwo));
  24. doit.insert(MapCall::value_type("Nil",DoOne));
  25.  
  26. MapCall::const_iterator call;
  27.  
  28. call = doit.find("One");
  29. if (call != doit.end())
  30. (*call).second();
  31. else
  32. cout << "Unknown call requested" << endl;
  33. }
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC