{"id":827,"date":"2020-10-30T17:02:00","date_gmt":"2020-10-30T09:02:00","guid":{"rendered":"http:\/\/www.wangkaixuan.tech\/?p=827"},"modified":"2020-11-04T14:34:25","modified_gmt":"2020-11-04T06:34:25","slug":"%e5%9b%9e%e8%b0%83%e5%9c%a8c-c%e4%b8%ad%e7%9a%84%e5%87%a0%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95%ef%bc%88%e9%99%84%e4%bb%a3%e7%a0%81%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=827","title":{"rendered":"\u56de\u8c03\u5728C\/C++\u4e2d\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff08\u9644\u4ee3\u7801\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u524d\u8a00<\/h2>\n\n\n\n<p>\u56de\u8c03\uff08callback\uff09\u662f\u5b9e\u73b0\u4ee3\u7801\u5e93\u529f\u80fd\u6269\u5c55\u7684\u4e00\u79cd\u91cd\u8981\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5e93\u7684\u5f00\u53d1\u8005\u9884\u7559\u4e86\u51e0\u4e2a\u529f\u80fd\u6269\u5c55\u70b9\uff0c\u5e76\u4ee5\u56de\u8c03\u63a5\u53e3\u7684\u5f62\u5f0f\u8fdb\u884c\u89c4\u5b9a\u3002\u4ee3\u7801\u5e93\u7684\u4f7f\u7528\u8005\u5b9e\u73b0\u56de\u8c03\u63a5\u53e3\uff0c\u5d4c\u5165\u81ea\u5df1\u7684\u6269\u5c55\u903b\u8f91\u3002\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u7528\u6237\u7684\u7a0b\u5e8f\u8c03\u7528\u4ee3\u7801\u5e93\uff0c\u4ee3\u7801\u5e93\u5728\u5408\u9002\u7684\u65f6\u5019\u518d\u53cd\u8fc7\u6765\u56de\u8c03\u5e94\u7528\u903b\u8f91\uff0c\u4e24\u8005\u914d\u5408\u5b9e\u73b0\u5b8c\u6574\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u4fe1\u53f7\u54cd\u5e94\u51fd\u6570\uff0cpthread_create\u4e2d\u7684\u7ebf\u7a0b\u51fd\u6570\u90fd\u662f\u56de\u8c03\u673a\u5236\u7684\u4e00\u79cd\u5e94\u7528\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u7528\u6237\u7684\u903b\u8f91\u4e0e\u4ee3\u7801\u5e93\u5176\u5b9e\u662f\u8fd0\u884c\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u7684\uff0c\u56e0\u6b64\u76f8\u6bd4\u6d88\u606f\u961f\u5217\u7b49\u529f\u80fd\u6269\u5c55\u624b\u6bb5\uff0c\u56de\u8c03\u673a\u5236\u5c31\u6709\u4e24\u4e2a\u7a81\u51fa\u7684\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u6027\u80fd\u66f4\u597d<\/strong>\uff1a\u4ee3\u7801\u5e93\u4e0e\u5e94\u7528\u903b\u8f91\u4e4b\u95f4\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u5171\u4eab\u6570\u636e\uff0c\u6570\u636e\u590d\u5236\u5f00\u9500\u5f88\u5c0f<\/li><li><strong>\u65b9\u4fbf\u63a7\u5236<\/strong>\uff1a\u4ee3\u7801\u5e93\u4e0e\u5e94\u7528\u903b\u8f91\u4e4b\u95f4\u662f\u540c\u6b65\u8c03\u7528\u7684\uff0c\u800c\u4e0d\u662f\u5f02\u6b65\u7684\uff0c\u65b9\u4fbf\u63a7\u5236\u8fd0\u884c\u65f6\u5e8f<\/li><\/ul>\n\n\n\n<p>\u5728C\/C++\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u9762\u53ef\u4ee5\u5b9e\u73b0\u56de\u8c03\uff0c\u4e0b\u9762\u4e00\u4e00\u8bf4\u660e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.C\u8bed\u8a00\u4e2d\u7684\u56de\u8c03<\/h2>\n\n\n\n<p>\u6765\u770b\u4ee3\u7801\uff08call_back_test_1.c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/compile with : gcc -o call_back_test_1 call_back_test_1.c \n\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n\n\/\/1.\u5b9a\u4e49\u56de\u8c03\u63a5\u53e3\ntypedef void (*CALLBACK_FUNC_NAME)(char* s_arg, int i_arg, double d_arg);\n\n\/\/2.\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\uff0c\u8ddf\u56de\u8c03\u63a5\u53e3\u7684\u51fd\u6570\u7b7e\u540d\u4e00\u81f4\nvoid test_callback_func(char* s_arg, int i_arg, double d_arg)\n{\n    printf(\"print from callback, s_arg = %s, i_arg = %d, d_arg = %lf\\n\",\n        s_arg, i_arg, d_arg);\n}\n\n\/\/3.\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u51fd\u6570\uff0c\u4f20\u5165\u56de\u8c03\u63a5\u53e3\nint event_loop(CALLBACK_FUNC_NAME callback_func_ptr)\n{\n    while(1)\n    {\n        callback_func_ptr(\"STRING\", 1, 0.1);\n        sleep(1);\n    }\n    return 0;\n}\n\nint main()\n{\n    \/\/\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af\n    return event_loop(test_callback_func);\n}<\/code><\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u903b\u8f91\u5f88\u7b80\u5355\uff0c\u770b\u6ce8\u91ca\u5c31\u80fd\u770b\u660e\u767d\uff0c\u4e0d\u518d\u89e3\u91ca\u3002<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"146\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-5.png\" alt=\"\" class=\"wp-image-828\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.C++\u4e2d\u7684\u56de\u8c03\uff08C99\u6807\u51c6\uff0c\u6210\u5458\u51fd\u6570\uff09<\/h2>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/compile with : g++ -o call_back_test_2 call_back_test_2.cpp \n\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n\n\/\/1.\u5b9a\u4e49\u56de\u8c03\u63a5\u53e3\ntypedef void (*CALLBACK_FUNC_NAME)(const char* s_arg, int i_arg, double d_arg);\n\n\/\/2.\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5185\u90e8\u5b9e\u73b0\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff0c\u5f53\u505a\u56de\u8c03\u51fd\u6570\u7528\nclass callback_t\n{\npublic:\n    callback_t()\n    {\n        \/\/m_idx = 0;\n    }\npublic:\n    static void test_callback_func(const char* s_arg, int i_arg, double d_arg)\n    {\n        printf(\"&#91;%ld]:print from callback, s_arg = %s, i_arg = %d, d_arg = %lf\\n\",\n            m_idx, s_arg, i_arg, d_arg);\n        \n        ++m_idx;\n    }\nprivate:\n    static long m_idx;\n};\n\nlong callback_t::m_idx = 0;\n\n\/\/3.\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u51fd\u6570\uff0c\u4f20\u5165\u56de\u8c03\u63a5\u53e3\nint event_loop(CALLBACK_FUNC_NAME callback_func_ptr)\n{\n    while(1)\n    {\n        callback_func_ptr(\"STRING\", 1, 0.1);\n        sleep(1);\n    }\n    return 0;\n}\n\nint main()\n{\n    \/\/\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u5bf9\u8c61\n    callback_t cb_obj;\n    \/\/\u4f20\u5165\u8fd9\u4e2a\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570\n    return event_loop(cb_obj.test_callback_func);\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n\n\n\n<p><strong>1.\u7c7b\u7684\u6210\u5458\u51fd\u6570\u5fc5\u987b\u5f97\u662fstatic\u7684\uff0c\u4e0d\u7136\u7f16\u8bd1\u4e0d\u8fc7<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"91\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-6.png\" alt=\"\" class=\"wp-image-829\"\/><\/figure>\n\n\n\n<p>\u56e0\u4e3a\u4eceC\u8bed\u8a00\u7ee7\u627f\u6765\u7684\u8fd9\u79cd\u65b9\u6cd5\uff0c\u8981\u6c42\u56de\u8c03\u51fd\u6570\u5fc5\u987b\u4f4d\u4e8e\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u666e\u901a\u7684\u7c7b\u6210\u5458\u51fd\u6570\u662f\u65e0\u6cd5\u6ee1\u8db3\u8981\u6c42\u7684\uff0c\u53ea\u6709\u9759\u6001\u6210\u5458\u51fd\u6570\u624d\u884c\u3002<\/p>\n\n\n\n<p>\u800c\u89c4\u5b9a\u5fc5\u987b\u4f7f\u7528\u9759\u6001\u7684\u6210\u5458\u51fd\u6570\uff0c\u5176\u5b9e\u57fa\u672c\u4e0a\u7b49\u540c\u4e8e\u5c06C++\u5f53C\u7528\uff0c\u9762\u5411\u5bf9\u8c61\u7684\u8bf8\u591a\u597d\u5904\u90fd\u65e0\u6cd5\u53d1\u6325\uff0c\u56e0\u6b64\u9664\u975e\u4e07\u4e0d\u5f97\u5df2\uff0c\u4e0d\u8981\u4f7f\u7528\u8fd9\u79cd\u56de\u8c03\u673a\u5236\u3002<\/p>\n\n\n\n<p><strong>2.\u4f20\u5165\u56de\u8c03\u51fd\u6570\u662f\u65e0\u9700\u521b\u5efa\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u7c7b\u7684\u9759\u6001\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u65f6\u65e0\u9700\u521b\u5efa\u5bf9\u8c61\uff0c\u76f4\u63a5\u7528\u7c7b\u540d\u505a\u9650\u5b9a\u7b26\u53d6\u9759\u6001\u6210\u5458\u51fd\u6570\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    \/\/\u4f20\u5165\u8fd9\u4e2a\u9759\u6001\u6210\u5458\u51fd\u6570\n    return event_loop(callback_t::test_callback_func);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.C++\u4e2d\u7684\u56de\u8c03\uff08C99\u6807\u51c6\uff0c\u56de\u8c03\u7c7b\uff09<\/h2>\n\n\n\n<p>\u76f4\u63a5\u770b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/compile with : g++ -o call_back_test_3 call_back_test_3.cpp \n\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n\n\/\/1.\u5b9a\u4e49\u56de\u8c03\u63a5\u53e3\u7c7b\nclass callback_if_t\n{\npublic:\n    void virtual callback_func(const char* s_arg, int i_arg, double d_arg) = 0;\n};\n\n\/\/2.\u5b9e\u73b0\u56de\u8c03\u63a5\u53e3\u7c7b\u4e2d\u89c4\u5b9a\u7684\u63a5\u53e3\nclass callback_impl_t: public callback_if_t\n{\npublic:\n    void virtual callback_func(const char* s_arg, int i_arg, double d_arg)\n    {\n        printf(\"print from callback, s_arg = %s, i_arg = %d, d_arg = %lf\\n\",\n            s_arg, i_arg, d_arg);\n    }\n};\n\n\/\/3.\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u51fd\u6570\uff0c\u4f20\u5165\u56de\u8c03\u63a5\u53e3\u7c7b\nint event_loop(callback_if_t *p_callback_if)\n{\n    while(1)\n    {\n        p_callback_if->callback_func(\"STRING\", 1, 0.1);\n        sleep(1);\n    }\n    return 0;\n}\n\nint main()\n{\n    \/\/\u5b9e\u4f8b\u5316\u63a5\u53e3\u7c7b\u5b9e\u73b0\u5bf9\u8c61\n    callback_impl_t callback_imp;\n    \/\/\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af\n    return event_loop(&amp;callback_imp);\n}\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u5c31\u597d\u5f88\u591a\uff0c\u5728\u5b9e\u9645\u751f\u4ea7\u4e2d\u5e94\u7528\u4e5f\u6bd4\u8f83\u5e7f\u6cdb\u3002<\/p>\n\n\n\n<p>\u76f8\u6bd4\u4e0a\u4e2a\u4f8b\u5b50\u4e2d\u76f4\u63a5\u628aC++\u5f53C\u7528\uff0c\u8fd9\u91cc\u81f3\u5c11\u7528\u5230\u4e86<strong>\u6cdb\u578b<\/strong>\uff0c\u503c\u5f97\u5b89\u6170\u4e0b\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56de\u8c03\u7684\u63a5\u53e3\u7528\u4e00\u4e2a\u7c7b\u5305\u88c5\u8d77\u6765\uff0c\u63a5\u53e3\u5168\u90e8\u5b9a\u4e49\u4e3a\u7eaf\u865a\u51fd\u6570\u3002\u4e1a\u52a1\u903b\u8f91\u7c7b\u4ece\u56de\u8c03\u63a5\u53e3\u7c7b\u7ee7\u627f\uff0c\u5b9e\u73b0\u5176\u7eaf\u865a\u51fd\u6570\uff0c\u5d4c\u5165\u4e1a\u52a1\u903b\u8f91\u3002\u5e93\u8fdb\u884c\u56de\u8c03\u7684\u65f6\u5019\uff0cC++\u7684\u6cdb\u578b\u673a\u5236\u4f1a\u4fdd\u8bc1\u5176\u8c03\u7528\u5230\u6b63\u786e\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u5e94\u7528\u5e7f\u6cdb\uff0c\u4f46\u4e5f\u4e0d\u662f\u6ca1\u6709\u7f3a\u70b9\u3002\u8bd5\u60f3\u5982\u679c\u4f60\u60f3<strong>\u589e\u52a0\u4e00\u4e2a\u63a5\u53e3<\/strong>\uff0c\u8fd9\u4e2a\u7c7b\u7684\u7b26\u53f7\u5c31\u53d8\u4e86\uff0c\u65b0\u7684\u63a5\u53e3\u7c7b\u642d\u914d\u8001\u7684\u5b9e\u73b0\u7c7b\u5fc5\u7136\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u63a5\u53e3\u5176\u5b9e\u662f\u628a\u5404\u4e2a\u5c0f\u7684\u51fd\u6570\uff08\u6ce5\u56e2\uff09\u6253\u5305\u6210\u4e86\u5927\u6ce5\u56e2\uff0c\u76f8\u6bd4UNIX\/Linux\u5f0f\u7684\u7eafC\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\uff0c\u6709\u79cd\u53cd\u5176\u9053\u800c\u884c\u4e4b\u7684\u611f\u89c9\uff0c\u4e0d\u591f\u7b80\u6d01\u4f18\u96c5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.C++\u4e2d\u7684\u56de\u8c03\uff08C++11\u6807\u51c6\uff0c\u56de\u8c03\u7c7b\uff09<\/h2>\n\n\n\n<p>\u5230\u4e86C++11\uff0c\u7ec8\u4e8e\u770b\u5230\u4e86\u66d9\u5149\uff0c\u56e0\u4e3a\u8fd9\u4e00\u7248C++\u5f15\u5165\u4e86function\u3001bind\u3001lambda\u4e09\u4e2a\u5927\u6740\u5668\u3002<\/p>\n\n\n\n<p>\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u770b\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/compile with : g++ -o call_back_test_4 call_back_test_4.cpp --std=c++11 \n\n#include &lt;functional>\n\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n\n\/\/ 1. \u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u7c7b\u578b\uff08std::function\uff09\ntypedef std::function&lt;void(const char*, int, double)> call_back_func_t;\n\n\/\/ 2.1 \u666e\u901a\u7684C-style\u7684\u56de\u8c03\u51fd\u6570\nvoid c_callback_func(const char* s_arg, int i_arg, double d_arg)\n{\n    printf(\"print from c_callback_func, s_arg = %s, i_arg = %d, d_arg = %lf\\n\",\n        s_arg, i_arg, d_arg);\n}\n\n\/\/ 2.2 C++\u7c7b\u6210\u5458\u53d8\u91cf\u56de\u8c03\u51fd\u6570\nclass cpp_callback_t\n{\npublic:\n    void cpp_callback_func(const char* s_arg, int i_arg, double d_arg)\n    {\n        printf(\"print from cpp_callback_func, s_arg = %s, i_arg = %d, d_arg = %lf\\n\",\n            s_arg, i_arg, d_arg);\n    }\n};\n\n\/\/ 3. \u4e8b\u4ef6\u5faa\u73af\uff08\u53ef\u63a5\u53d7\u53c2\u6570\u4e3astd::function\uff09\nint event_loop(call_back_func_t cb_func)\n{\n    int loop_times = 5;\n    while(loop_times--)\n    {\n        if(cb_func)\n            cb_func(\"STRING\", 1, 0.1);\n        sleep(1);\n    }\n    return 0;\n}\n\nint main()\n{\n    \/\/std::fcuntion\u7c7b\u578b\u7684\u53d8\u91cf\n    call_back_func_t cb_func;\n    \n    \/\/ 1. \u53ef\u63a5\u53d7\u666e\u901aC-style\u56de\u8c03\u51fd\u6570\n    cb_func = c_callback_func;\n    event_loop(cb_func);\n    printf(\"------------------\\n\");\n\n    \/\/ 2.1 \u53ef\u901a\u8fc7std::bind\u7ed1\u5b9a\u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\uff08\u7ed1\u5b9a\u65b9\u5f0f1\uff09\n    cpp_callback_t cpp_cb;\n    cb_func = std::bind(&amp;cpp_callback_t::cpp_callback_func, cpp_cb, \n        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);\n    event_loop(cb_func);\n    printf(\"------------------\\n\");\n\n    \/\/ 2.2 \u53ef\u901a\u8fc7std::bind\u7ed1\u5b9a\u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\uff08\u7ed1\u5b9a\u65b9\u5f0f2\uff09\n    typedef std::function&lt;void(cpp_callback_t, const char*, int, double)> call_back_func_t_2;\n    call_back_func_t_2 cb_func_2 = &amp;cpp_callback_t::cpp_callback_func;\n    cb_func_2(cpp_cb, \"STRING_2\", 1, 0.1);\n    printf(\"------------------\\n\");\n\n    \/\/ 3. \u8fd8\u80fd\u7ed1\u5b9a\u4e00\u4e2alambda\u51fd\u6570\uff01\uff01\uff01\n    auto lambda_cb_func = &#91;](const char* s_arg, int i_arg, double d_arg)->void\n        {\n            printf(\"print from lambda_callback_func, s_arg = %s, i_arg = %d, d_arg = %lf\\n\",\n                s_arg, i_arg, d_arg);\n        };\n    cb_func = lambda_cb_func;\n    event_loop(cb_func);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528std::function\u58f0\u660e\u56de\u8c03\u51fd\u6570\u7c7b\u578b\uff0c\u5149\u662f\u8fd9\u4e2a\u53c2\u6570\u5217\u8868\u5c31\u7b80\u6d01\u4e0d\u5c11\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"47\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-7.png\" alt=\"\" class=\"wp-image-832\"\/><\/figure>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u4ee5\u5404\u79cd\u683c\u5f0f\u5b9e\u73b0\u4e86\u56de\u8c03\u51fd\u6570\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<p>\u666e\u901a\u7684C\u8bed\u8a00\u98ce\u683c\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"128\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-8.png\" alt=\"\" class=\"wp-image-833\"\/><\/figure>\n\n\n\n<p>\u8fd9\u79cd\u51fd\u6570\u65e0\u9700\u7279\u6b8a\u7ed1\u5b9a\uff0c\u80fd\u76f4\u63a5\u590d\u5236\u7ed9std::function\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>C++\uff08\u975e\u9759\u6001\uff09\u6210\u5458\u51fd\u6570\u98ce\u683c\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"203\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-9.png\" alt=\"\" class=\"wp-image-834\"\/><\/figure>\n\n\n\n<p>\u8fd9\u79cd\u51fd\u6570\u9700\u8981\u901a\u8fc7std:bind\u63a5\u53e3\u624d\u80fd\u8d4b\u503c\u7ed9std::function\u7c7b\u578b\u7684\u53d8\u91cf\u3002bind\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662fbind\u65f6\u6307\u5b9a\u7c7b\u5bf9\u8c61\uff0c\u8c03\u7528\u65f6\u4e0d\u6307\u5b9a\uff1b\u4e00\u79cd\u662fbind\u65f6\u4e0d\u6307\u5b9a\u7c7b\u5bf9\u8c61\uff0c\u8c03\u7528\u65f6\u6307\u5b9a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"243\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-11.png\" alt=\"\" class=\"wp-image-836\"\/><\/figure>\n\n\n\n<p>\u66f4\u60ca\u559c\u7684\u662f\uff0c\u4ed6\u8fd8\u80fd\u7ed1\u5b9alambda\u51fd\u6570\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"164\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-12.png\" alt=\"\" class=\"wp-image-837\"\/><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"321\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/10\/image-13.png\" alt=\"\" class=\"wp-image-838\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">6.\u603b\u7ed3<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u7acb\u5fd7\u5199\u73b0\u4ee3C++\u7a0b\u5e8f\uff0c\u90a3\u4e48\u5f3a\u70c8\u5efa\u8bae\u4f60\u9009\u62e9C++11\u98ce\u683c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1b\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u4fdd\u6301\u8f83\u597d\u7684\u517c\u5bb9\u6027\uff0c\u6216\u8005\u53ea\u80fd\u7528C\u8bed\u8a00\uff0c\u90a3\u4e48\u4f60\u53ea\u80fd\u9009\u62e9C\u8bed\u8a00\u98ce\u683c\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e86\u3002\u53e6\u5916\u4e24\u79cd\uff0c\u4e00\u4e2a\u4e0d\u4f26\u4e0d\u7c7b\uff0c\u4e00\u4e2a\u96be\u4ee5\u5347\u7ea7\uff0c\u80fd\u4e0d\u7528\u5c31\u4e0d\u7528\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u524d\u8a00 \u56de\u8c03\uff08callback\uff09\u662f\u5b9e\u73b0\u4ee3\u7801\u5e93\u529f\u80fd\u6269\u5c55\u7684\u4e00\u79cd\u91cd\u8981\u673a\u5236\u3002 \u4ee3\u7801\u5e93\u7684\u5f00\u53d1\u8005\u9884\u7559\u4e86\u51e0\u4e2a\u529f\u80fd\u6269\u5c55\u70b9\uff0c\u5e76\u4ee5\u56de\u8c03\u63a5\u53e3\u7684\u5f62\u5f0f\u8fdb\u884c\u89c4\u5b9a\u3002\u4ee3\u7801\u5e93\u7684\u4f7f\u7528\u8005\u5b9e\u73b0\u56de\u8c03\u63a5&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=827\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-827","post","type-post","status-publish","format-standard","hentry","category-05-01-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=827"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/827\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}