Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create map<string, class::method> in c++ and be able to search for function and call it?

Tags:

I'm trying to create a map of string and method in C++, but I don't know how to do it. I would like to do something like that (pseudocode):

map<string, method> mapping = {   "sin", Math::sinFunc,   "cos", Math::cosFunc,   ... };  ...  string &function; handler = mapping.find(function); int result;  if (handler != NULL)   result = (int) handler(20); 

To be honest I don't know is it possible in C++. I would like to have a map of string, method and be able to search for function in my mapping. If given string name of function exists then I would like to call it with given param.

like image 495
user360872 Avatar asked Jun 24 '10 19:06

user360872


People also ask

How do you put a string on a map of strings?

To insert the data in the map insert() function in the map is used. It is used to insert elements with a particular key in the map container. Parameters: It accepts a pair that consists of a key and element which is to be inserted into the map container but it only inserts the unique key.

What is std::map in C++?

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare . Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.

Can a string be a key in map C++?

A map is an associative container that maps keys to values, provides logarithmic complexity for inserting and finding, and constant time for erasing single elements. It is common for developers to use a map to keep track of objects by using a string key.

Can we map a string?

8 Answers. Show activity on this post. Then you can create a Map<String,ComputeString> object like you want in the first place. Using a map will be much faster than reflection and will also give more type-safety, so I would advise the above.


2 Answers

Well, I'm not a member of the popular here Boost Lovers Club, so here it goes - in raw C++.

#include <map> #include <string>  struct Math {     double sinFunc(double x) { return 0.33; };     double cosFunc(double x) { return 0.66; }; };  typedef double (Math::*math_method_t)(double); typedef std::map<std::string, math_method_t> math_func_map_t;  int main() {      math_func_map_t mapping;     mapping["sin"] = &Math::sinFunc;     mapping["cos"] = &Math::cosFunc;      std::string function = std::string("sin");     math_func_map_t::iterator x = mapping.find(function);     int result = 0;      if (x != mapping.end()) {         Math m;         result = (m.*(x->second))(20);     } } 

That's obviously if I have understood correctly that you want a method pointer, not a function/static method pointer.

like image 171
Dummy00001 Avatar answered Sep 19 '22 15:09

Dummy00001


This is indeed possible in C++, thanks to function pointers. Here's a simple example:

  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;       }   } 

This gets more tricky when dealing with functions with different return types and arguments. Also, if you are including non-static member functions, you should use Boost.Function.

like image 24
Justin Ardini Avatar answered Sep 21 '22 15:09

Justin Ardini