As for std::map to place the pointers to functions in the class?

Suppose I have a class
class Foo
{
public:
 static Foo *fromFoo();
 static Foo *fromBar();
};


How to create a associative array that will contain pointers to functions, so I could dynamically create objects from a string, without the noodles of the form if(str == "foo") {} ....
October 3rd 19 at 04:29
4 answers
October 3rd 19 at 04:31
May seem boring, but Boost::Function not?
Will not seem, it is certainly better.
Let me explain to those who vote for a solution with a function pointer.
To choose boost::function is how to choose std::string instead of char *. - Joseph.Gorczany99 commented on October 3rd 19 at 04:34
Choose to store in a std::map to a function pointer is as bad as storing a char * instead of std::string
That is with boost::function will be std::map to store anything.
map["fromFoo"] = &Foo::fromFoo;
map["fromBar"] = boost::bind(....)
- Joseph.Gorczany99 commented on October 3rd 19 at 04:37
October 3rd 19 at 04:33
Static methods are normal functions.

typedef (Foo*) (*func)();
std::map<std::string, func> map;
map["fromFoo"] = &Foo::fromFoo;
map["fromBar"] = &Foo::fromBar;
</std::string,>
Kind of clear...
Initialize and map container to execute you? As with standard arrays? - Joseph.Gorczany99 commented on October 3rd 19 at 04:36
No. std::map so is not able because it stores data in dynamic memory. Want to make a global singleton object? - Joseph.Gorczany99 commented on October 3rd 19 at 04:39
you can, but it will look gross.
For example:
 pair<string, int> arr [] = {make_pair("opsa", 1), make_pair("dripsa", 2)};
 map<string, int> m(&arr[0], &arr[3]);
</string></string>
- buck commented on October 3rd 19 at 04:42
First, you are not disgusting:
map<string, int> m({{"opsa", 1}, {"dripsa", 2}})
</string>

Second is the initialization execution time, not compile-time. - Missouri_OKeefe12 commented on October 3rd 19 at 04:45
1. Is C++11
2. of course performance. the designer, after all. But I realized that comrade want a declarative all his household to link. for these purposes fit. to initialize all static. - Ross67 commented on October 3rd 19 at 04:48
1) since the middle of 2013 in the yard. It's time to use C++11 with plenty of chips.
2) But people have asked about the initialization of compile-time. And globalname objects need to be careful not to use the uninitialized object. - Missouri_OKeefe12 commented on October 3rd 19 at 04:51
Sorry in the pros there is no support for switch-case for non-constant values, they were easy to convert if-elseif - Ross67 commented on October 3rd 19 at 04:54
October 3rd 19 at 04:35
int (TMyClass::*pt2Member)(float, char, char) = &TMyClass::DoIt
Hence, something wrong? With static the problem?
October 3rd 19 at 04:37
Well, you can also in the map to hold pointers, not to function as a class factory. Or should that way — through function?
And what function is not factory? This is a Java factory is definitely the class because there are only classes are. And in C++ if the condition is not necessary, but you want to encapsulate the logic of creating the object, you can function or static method to do. - Joseph.Gorczany99 commented on October 3rd 19 at 04:40

Find more questions by tags C++OOP