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.
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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With