Why the switch statement cannot be applied on strings?

前端 未结 20 2587
离开以前
离开以前 2020-11-22 03:58

Compiling the following code and got the error of type illegal.

int main()
{
    // Compilation error - switch expression of type illegal
    sw         


        
20条回答
  •  长发绾君心
    2020-11-22 04:22

    More functional workaround to the switch problem:

    class APIHandlerImpl
    {
    
    // define map of "cases"
    std::map> in_events;
    
    public:
        APIHandlerImpl()
        {
            // bind handler method in constructor
            in_events["/hello"] = std::bind(&APIHandlerImpl::handleHello, this, _1, _2, _3);
            in_events["/bye"] = std::bind(&APIHandlerImpl::handleBye, this, _1, _2, _3);
        }
    
        void onEvent(string event = "/hello", string data = "{}")
        {
            // execute event based on incomming event
            in_events[event](s, hdl, data);
        }
    
        void APIHandlerImpl::handleHello(server* s, websocketpp::connection_hdl hdl, string data)
        {
            // ...
        }
    
        void APIHandlerImpl::handleBye(server* s, websocketpp::connection_hdl hdl, string data)
        {
            // ...
        }
    }
    

提交回复
热议问题