{"id":351,"date":"2021-06-28T15:52:04","date_gmt":"2021-06-28T07:52:04","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=351"},"modified":"2025-10-09T13:56:04","modified_gmt":"2025-10-09T05:56:04","slug":"map-of-member-function-pointers","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/28\/map-of-member-function-pointers\/","title":{"rendered":"C++ \u5c06\u6210\u5458\u51fd\u6570\u6620\u5c04\u5230map\u4e0a"},"content":{"rendered":"<p>&emsp;&emsp;\u6211\u4eec\u5e0c\u671b\u5c06\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u51fd\u6570\u5c01\u88c5\u5230\u4e00\u4e2amap\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684\u505a\u6cd5\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff0c\u7ed1\u5b9a\u7684\u51fd\u6570\u4ed6\u4eec\u9700\u8981\u6709\u76f8\u540c\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u4ee5\u53ca\u76f8\u540c\u7684\u53c2\u6570\u5217\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;string&gt;\n\n\/\/ \u4e00\u4e2a\u7c7b\nclass OneClass\n{\nprivate:\n\/\/ ===== \u79c1\u6709\u6210\u5458\u53d8\u91cf =====   \n    \/\/ using _Name_ = Func_Type(Class::*)(Parameters...)\n    using MFP = int(OneClass::*)(int param);\n\n    std::map&lt;std::string, MFP&gt; m_funcMap;\n\n\/\/ ===== \u79c1\u6709\u6210\u5458\u51fd\u6570 =====\n    int AddOne(int p)\n    {\n        return p + 1;\n    }\n\n    int AddTwo(int p)\n    {\n        return p + 2;\n    }\npublic:\n\/\/ ===== \u516c\u6709\u6210\u5458\u51fd\u6570 =====   \n    OneClass()\n    {\n        \/\/ \u521d\u59cb\u5316\n        this-&gt;m_funcMap[&quot;AddOne&quot;] = &amp;OneClass::AddOne;\n        this-&gt;m_funcMap[&quot;AddTwo&quot;] = &amp;OneClass::AddTwo;\n    }\n\n    int CallFunction(std::string str, int p)\n    {\n        MFP fp = this-&gt;m_funcMap[str];\n\n        return (this-&gt;*fp)(p);\n    }\n\n};\n\nint main()\n{\n    OneClass c;\n\n    std::cout &lt;&lt; c.CallFunction(&quot;AddOne&quot;, 3) &lt;&lt; std::endl;\n\n    std::cout &lt;&lt; c.CallFunction(&quot;AddTwo&quot;, 3) &lt;&lt; std::endl;\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;\u6211\u4eec\u5e0c\u671b\u5c06\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u51fd\u6570\u5c01\u88c5\u5230\u4e00\u4e2amap\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684\u505a\u6cd5\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff0c\u7ed1\u5b9a\u7684\u51fd\u6570\u4ed6\u4eec\u9700\u8981\u6709\u76f8 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[14],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/351"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":2,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/351\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}