How to create map in c++ and be able to search for function and call it?

前端 未结 7 906
予麋鹿
予麋鹿 2021-01-01 15:25

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

        
7条回答
  •  囚心锁ツ
    2021-01-01 15:56

    //pick one
    typedef float (*func_type_1)(float);
    typedef boost::function func_type_2;
    
    std::map fm;
    fm["sin"] = &Math::sin;
    fm["cos"] = &Math::cos;
    
    auto f = fm[str];
    result = f(42);
    

提交回复
热议问题