{"id":691,"date":"2023-04-03T10:00:33","date_gmt":"2023-04-03T02:00:33","guid":{"rendered":"http:\/\/www.inhhh.com\/blog\/?p=691"},"modified":"2023-04-03T10:00:33","modified_gmt":"2023-04-03T02:00:33","slug":"c-%e5%87%bd%e6%95%b0%e6%8c%87%e9%92%88%e5%88%86%e9%85%8d%e6%97%a0%e6%b3%95%e8%bd%ac%e6%8d%a2%e7%b1%bb%e5%86%85%e7%9a%84%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/www.inhhh.com\/blog\/?p=691","title":{"rendered":"C ++\u51fd\u6570\u6307\u9488\u5206\u914d\u65e0\u6cd5\u8f6c\u6362\u7c7b\u5185\u7684\u7c7b\u578b"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.codenong.com\/tag\/c%2B%2B\/\">c++<\/a><a href=\"https:\/\/www.codenong.com\/tag\/class\/\">class<\/a><a href=\"https:\/\/www.codenong.com\/tag\/function\/\">function<\/a><a href=\"https:\/\/www.codenong.com\/tag\/pointers\/\">pointers<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C++ Function Pointer assignment cannot convert types inside a class<\/h2>\n\n\n\n<p>\u672c\u95ee\u9898\u5df2\u7ecf\u6709\u6700\u4f73\u7b54\u6848\uff0c\u8bf7<a target=\"_blank\" href=\"https:\/\/www.codenong.com\/2402579\/\" rel=\"noreferrer noopener\">\u731b\u70b9\u8fd9\u91cc\u8bbf\u95ee\u3002<\/a><\/p>\n\n\n\n<p>\u6211\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\uff0c\u8be5\u7c7b\u5177\u6709\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u3002 \u6784\u9020\u51fd\u6570\u5c06\u5176\u6307\u5411\u51fd\u6570&#8221; morning()&#8221;\uff0c\u5e76\u4e14\u5728\u7f16\u8bd1\u65f6\u6536\u5230\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<br>2<\/td><td>error:&nbsp;cannot convert \u2018Test::morning\u2019 from type \u2018void&nbsp;(Test::)()\u2019 to type \u2018Test::function&nbsp;{aka&nbsp;void&nbsp;(*)()}\u2019<br>&nbsp; &nbsp; &nbsp;Test()&nbsp;{assignPtr&nbsp;=&nbsp;morning;}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5f53\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e&#8221; morning()&#8221;\u548ctypedef\u65f6\uff0c\u4ee3\u7801\u4f1a\u7f16\u8bd1\uff0c\u800c\u6211\u65e0\u6cd5\u5f04\u6e05\u695a\u5982\u4f55\u4f7f\u5176\u5728\u5f53\u524d\u7c7b\u4e2d\u5de5\u4f5c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<\/td><td><em>#include &lt;iostream&gt;<\/em><br><em>#include &lt;string&gt;<\/em>class&nbsp;Test&nbsp;{<br>&nbsp;public:<br>&nbsp; &nbsp; Test()&nbsp;{assignPtr&nbsp;=&nbsp;morning;}<br>&nbsp;void&nbsp;say(std::string&nbsp;a)&nbsp;{name&nbsp;=&nbsp;a;&nbsp;assignPtr();};<br>&nbsp;private:<br>&nbsp;typedef&nbsp;void(*function)();<br>&nbsp;void&nbsp;morning()&nbsp;{std::cout&nbsp;&lt;&lt;&#8220;Good morning,&#8221;&nbsp;&lt;&lt;&nbsp;name&nbsp;&lt;&lt;&nbsp;std::endl;}<br>&nbsp;void&nbsp;night()&nbsp;{};<br>&nbsp; &nbsp; function assignPtr;<br>&nbsp; &nbsp; std::string&nbsp;name;<br>};int&nbsp;main()&nbsp;{<br>&nbsp; &nbsp; Test a;<br>&nbsp; &nbsp; a.say(&#8220;Miguel&#8221;);<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488\u4e0d\u662f\u5e38\u89c4\u51fd\u6570\u6307\u9488\u3002 \u8be5\u7c7b\u662f\u8be5\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u8981\u4f7f\u7528\u54ea\u4e2a\u5bf9\u8c61\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u60a8\u9700\u8981\u4f7f\u7528\u6b63\u786e\u7684\u7c7b\u578b<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>typedef&nbsp;void(Test::*function)();<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7136\u540e\u5728\u521b\u5efa\u6307\u9488\u548c\u8c03\u7528\u6307\u9488\u65f6\u90fd\u8c03\u6574\u8bed\u6cd5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<br>2<\/td><td>Test()&nbsp;{assignPtr&nbsp;=&nbsp;&amp;Test::morning;}&nbsp;<em>\/\/ Get pointer-to-member<\/em><br>void&nbsp;say(std::string&nbsp;a)&nbsp;{name&nbsp;=&nbsp;a;&nbsp;(this-&gt;*assignPtr)();};&nbsp;<em>\/\/ Use pointer-to-member<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u51fd\u6570\u6307\u9488\u548c\u6210\u5458\u51fd\u6570\u6307\u9488\u4e0d\u662f\u4e00\u56de\u4e8b\u3002 \u90a3\u5c31\u662f\u7f16\u8bd1\u5668\u544a\u8bc9\u60a8\u7684\u5185\u5bb9\u3002 \u7c7b\u578b\u4e0d\u5339\u914d\u3002 \u60a8\u7684\u76ee\u6807\u5b9a\u4e49\u4e3avoid(*)()\uff0c\u800c\u60a8\u6b63\u5c1d\u8bd5\u5c06\u5176\u4ea4\u4ed8\u7ed9void(Test::*)()\u3002 \u5b83\u4eec\u751a\u81f3\u6ca1\u6709\u76f8\u540c\u7684\u8c03\u7528\u8bed\u4e49\u3002 \u540e\u8005\u5177\u6709Test*const this\u76f2\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u66f4\u6539function\u7684\u7c7b\u578b\u6216\u4e3a\u5176\u63d0\u4f9b\u5e38\u89c4\u51fd\u6570\u4ee5\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u63d0\u793a\uff1a\u4efb\u4f55\u65f6\u5019\u60a8\u7684\u7f16\u8bd1\u5668\u56e0\u65e0\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u8be5\u9519\u8bef\u800c\u62b1\u6028&#8230;\u60a8\u7684\u7c7b\u578b\u4e0d\u5339\u914d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>c++classfunctionpointers C++ Function Pointer assignmen<\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"http:\/\/www.inhhh.com\/blog\/?p=691\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=691"}],"version-history":[{"count":1,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":692,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions\/692"}],"wp:attachment":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}