N
N
Nikita Gusakov2013-07-25 04:50:01
OOP
Nikita Gusakov, 2013-07-25 04:50:01

How to put pointers to class functions in std::map?

Let's say I have a class

class Foo
{
public:
    static Foo *fromFoo();
    static Foo *fromBar();
};

How do I create such an associative array that will contain function pointers so that I can dynamically create objects from a string, without the noodles of the formif(str == "foo") {} ....

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Fedin, 2013-07-25
@zloiia

I may seem like a bore, but Boost:: Function is not?

M
mayorovp, 2013-07-25
@mayorovp

Static methods are regular functions.

typedef (Foo*) (*func)();
std::map<std::string, func> map;
map["fromFoo"] = &Foo::fromFoo;
map["fromBar"] = &Foo::fromBar;

A
Andrew, 2013-07-25
@xaoc80

Well, you can keep in the map not pointers to a function, but to a class factory. Or is it necessary just like that - through functions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question