- class CMyclass
- {
- public:
- void DoTest(const std::string& input_url) {
- std::function<void (void)> load_parser_function_bind_urlparam;
- std::function<void (const char*)> load_parser_function;
- auto head = input_url.substr(0, 4);
- if (head.compare("rtsp") == 0) {
- load_parser_function_bind_urlparam = std::bind(&CMyclass::ParserRTSPUrl, this, input_url.c_str());
- load_parser_function = std::bind(&CMyclass::ParserRTSPUrl, this, std::placeholders::_1);
- } else if (head.compare("rtmp") == 0) {
- load_parser_function_bind_urlparam = std::bind(&CMyclass::ParserRTMPUrl, this, input_url.c_str());
- load_parser_function = std::bind(&CMyclass::ParserRTMPUrl, this, std::placeholders::_1);
- } else {
- load_parser_function_bind_urlparam = std::bind(&CMyclass::ParserMMSUrl, this, input_url.c_str());
- load_parser_function = std::bind(&CMyclass::ParserMMSUrl, this, std::placeholders::_1);
- }
- //do other process
- //if (xxxx != 12) {
- // return ;
- //}
- //call ParserXXX function without params
- load_parser_function_bind_urlparam();
- //call ParserXXX function with params
- load_parser_function(input_url.c_str());
- AtlTrace("[CMyclass::DoWord()] end\\n");
- }
- private:
- void ParserRTSPUrl(const char* purl) {
- AtlTrace("[ParserRTSPUrl] %s\\n", purl);
- //do something
- }
- void ParserRTMPUrl(const char* purl) {
- AtlTrace("[ParserRTMPUrl] %s\\n", purl);
- //do something
- }
- void ParserMMSUrl(const char* purl) {
- AtlTrace("[ParserMMSUrl] %s\\n", purl);
- //do something
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- CMyclass my_class;
- my_class.DoTest("rtsp://xxx");
- my_class.DoTest("rtmp://xxx");
- my_class.DoTest("mms://xxx");
- return 0;
- }
- 输出:
- [ParserRTSPUrl] rtsp://xxx
- [ParserRTSPUrl] rtsp://xxx
- [CMyclass::DoWord()] end
- [ParserRTMPUrl] rtmp://xxx
- [ParserRTMPUrl] rtmp://xxx
- [CMyclass::DoWord()] end
- [ParserMMSUrl] mms://xxx
- [ParserMMSUrl] mms://xxx
- [CMyclass::DoWord()] end
- //该片段来自于http://www.codesnippet.cn/detail/161220138051.html
来源: http://www.codesnippet.cn/detail/161220138051.html