mardi 16 avril 2019

why do i can't do this in c++

This question has nothing to do with logic. I have set up a pair container, which is defined by (c style), and I want to insert it into the map later. But once I leave this line (pair protocals[] ), the contents will be destroyed immediately. I tried two ways. Will not work. I want to know why? thank you all


#define PRO_HELP_ALL(str, _bool, data, func) make_pair(cStrMsg(str, _bool), cUartMsg::sgArm2Mcu(data, func()))
#define PRO_HELP_HAVE(str, data, func) PRO_HELP_ALL(str,true,data,func)

cSocketManager::cSocketManager() {
// NO WORK
    pair<cStrMsg const &, cUartMsg const &> protocals[] = {
            PRO_HELP_HAVE(playCmd, playData, onPlayData),
            PRO_HELP_HAVE(playCur, playCurData, onPlayCur),
            PRO_HELP_HAVE(playSong, playSongData, onPlaySong),
    };
    for (int i = 0; i < sizeof(protocals) / sizeof(protocals[0]); ++i) {
        m_mapSearch.emplace(protocals[i]);
    }
// NO WORK TOO
//    vector<pair<cStrMsg const &, cUartMsg const &>> protocals{
//            PRO_HELP_HAVE(playCmd, playData, onPlayData),
//            PRO_HELP_HAVE(playCur, playCurData, onPlayCur),
//            PRO_HELP_HAVE(playSong, playSongData, onPlaySong),
//    };
//    for (auto p = protocals.begin(); p != protocals.end(); p++) {
//        m_mapSearch.insert(*p);
//    }

// WORK HERE
    m_mapSearch.insert(PRO_HELP_HAVE(playCur, playCurData, onPlayCur));
    m_mapSearch.insert(PRO_HELP_HAVE(playCmd, playData, onPlayData));
    m_mapSearch.insert(PRO_HELP_HAVE(playSong, playSongData, onPlaySong));
}

Aucun commentaire:

Enregistrer un commentaire