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

前端 未结 7 957
予麋鹿
予麋鹿 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 16:10

    The easiest way would be to use boost::function:

    #include 
    #include 
    #include 
    
    using namespace std;
    
    // later...
    
    map > funcs;
    funcs["sin"] = &Math::sinFunc;
    

    It gets slightly more complex if you're using member functions - boost::lambda can help:

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    namespace l = boost::lambda;
    
    // later...
    
    Math *m = new Math();
    map > funcs;
    funcs["sin"] = l::bind(&Math::sinFunc, m, l::_1);
    

提交回复
热议问题