{"id":241,"date":"2015-05-31T01:35:18","date_gmt":"2015-05-30T17:35:18","guid":{"rendered":"http:\/\/sqrt-1.me\/?p=241"},"modified":"2016-11-19T14:44:24","modified_gmt":"2016-11-19T06:44:24","slug":"c%e7%9a%84%e7%9b%b4%e6%8e%a5%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%8e%e5%a4%8d%e5%88%b6%e5%88%9d%e5%a7%8b%e5%8c%96","status":"publish","type":"post","link":"https:\/\/sqrt-1.me\/?p=241","title":{"rendered":"C++\u7684\u76f4\u63a5\u521d\u59cb\u5316\u4e0e\u590d\u5236\u521d\u59cb\u5316"},"content":{"rendered":"<p>C++\u4e2d\u7684<strong>\u76f4\u63a5\u521d\u59cb\u5316<\/strong>\u6307\u7684\u662f\u76f4\u63a5\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f8b\u5982<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nstring a; \/\/\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\r\nstring a(&quot;hello&quot;); \/\/\u8c03\u7528\u53c2\u6570\u4e3aconst char *\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\r\nstring b(a); \/\/\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\r\n<\/pre>\n<p><strong>\u590d\u5236\u521d\u59cb\u5316<\/strong>\u6307\u7684\u662f\u7528\u201c=\u201d\u53f7\u6765\u521d\u59cb\u5316\u5bf9\u8c61\uff0c\u4f8b\u5982<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nstring a=&quot;hello&quot;;\r\nstring b=a;\r\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e24\u79cd\u5199\u6cd5\u662f\u5b8c\u5168\u7b49\u6548\u7684\uff0c\u4f46\u662f\u76f4\u63a5\u521d\u59cb\u5316\u548c\u590d\u5236\u521d\u59cb\u5316\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u8fd8\u662f\u6709\u533a\u522b\u7684\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6839\u636eC++\u7684\u6807\u51c6\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u5c31\u662f\u76f4\u63a5\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\uff0c\u4f8b\u5982\u5728string a(&#8220;hello&#8221;)\u4e2d\uff0cstring\u7c7b\u7684string(const char *)\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0ca\u88ab\u76f4\u63a5\u521d\u59cb\u5316\u3002<\/p>\n<p>\u7136\u800c\u6839\u636e\u6807\u51c6\uff0c\u590d\u5236\u521d\u59cb\u5316\u5e94\u8be5\u662f\u5148\u8c03\u7528\u5bf9\u5e94\u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u518d\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5c06\u4e34\u65f6\u5bf9\u8c61\u62f7\u8d1d\u7ed9\u8981\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\u5728string a=&#8221;hello&#8221;\u4e2d\uff0cstring\u7c7b\u7684string(const char *)\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u9996\u5148\u8c03\u7528\uff0c\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5c06\u8fd9\u4e2a\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230a\u3002<\/p>\n<p>\u6807\u51c6\u8fd8\u89c4\u5b9a\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u5141\u8bb8\u7f16\u8bd1\u5668\u8df3\u8fc7\u521b\u5efa\u4e34\u65f6\u5bf9\u8c61\u8fd9\u4e00\u6b65\uff0c\u76f4\u63a5\u8c03\u7528\u6784\u9020\u51fd\u6570\u6784\u9020\u8981\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u5b8c\u5168\u7b49\u4ef7\u4e8e\u76f4\u63a5\u521d\u59cb\u5316\u4e86\u3002<\/p>\n<p>\u7ecf\u8fc7\u6211\u7684\u6d4b\u8bd5\uff0c\u5e38\u89c1\u7684\u7f16\u8bd1\u5668\uff08gcc\u548cVC++\uff09\u90fd\u4f1a\u76f4\u63a5\u8df3\u8fc7\u521b\u5efa\u4e34\u65f6\u5bf9\u8c61\u8fd9\u4e00\u6b65\uff0c\u5373\u4f7f\u6ca1\u6709\u6253\u5f00\u4efb\u4f55\u4f18\u5316\u9009\u9879\u3002<\/p>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u4e5f\u8bb8\u4f60\u5c31\u4f1a\u8ba4\u4e3a\u4ed6\u4eec\u5b8c\u5168\u7b49\u540c\u4e86\uff0c\u5176\u5b9e\u4e0d\u7136\u3002\u4e0b\u9762\u5206\u6790\u4e24\u79cd\u60c5\u51b5\u3002<\/p>\n<p><strong>1\u3001\u5f53\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e3aprivate\u65f6\u3002<\/strong><\/p>\n<p>\u867d\u7136\u7f16\u8bd1\u5668\u4f1a\u8df3\u8fc7\u521b\u5efa\u4e34\u65f6\u5bf9\u8c61\u8fd9\u4e00\u6b65\uff0c\u4f46\u662f\u8fd9\u4e2a\u7c7b\u5fc5\u987b\u8981\u80fd\u591f\u6b63\u786e\u7684\u8c03\u7528\u6307\u5b9a\u7684\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\u3002\u4f18\u5316\u662f\u7f16\u8bd1\u5668\u91c7\u53d6\u7684\u63aa\u65bd\uff0c\u4f46\u662f\u7a0b\u5e8f\u5fc5\u987b\u4f7f\u5f97\u4e0d\u4f18\u5316\u65f6\u4ee3\u7801\u4e5f\u7b26\u5408\u8bed\u6cd5\u3002<\/p>\n<p>C++\u4e2d\u6709\u4e00\u4e9b\u7cfb\u7edf\u7c7b\u578b\u4e0d\u5141\u8bb8\u590d\u5236\uff0c\u4f8b\u5982iostream\u548cfstream\u8fd9\u6837\u7684\u7c7b\u3002\u5982\u679c\u4f60\u60f3\u628a\u81ea\u5b9a\u4e49\u7684\u7c7b\u8bbe\u7f6e\u4e3a\u4e0d\u5141\u8bb8\u590d\u5236\uff0c\u628a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u58f0\u660e\u4e3aprivate\u5373\u53ef\uff0c\u524d\u9762\u6240\u8bf4\u7684\u7cfb\u7edf\u7c7b\u578b\u4e5f\u662f\u8fd9\u6837\u505a\u7684\u3002<\/p>\n<p>\u6211\u4eec\u7528ifstream\u4e3e\u4f8b\u8bf4\u660e\u76f4\u63a5\u521d\u59cb\u5316\u548c\u590d\u5236\u521d\u59cb\u5316\u7684\u533a\u522b\u3002\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u6784\u9020ifstream\u5bf9\u8c61\uff0c\u5373\u6253\u5f00\u4ee5\u5b57\u7b26\u4e32\u4e3a\u6587\u4ef6\u540d\u7684\u6587\u4ef6\u8f93\u5165\u6d41\u3002<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nifstream file(&quot;filename&quot;); \/\/\u7f16\u8bd1\u901a\u8fc7\r\nifstream file=&quot;filename&quot;; \/\/\u7f16\u8bd1\u51fa\u9519\uff0cifstream\u7c7b\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e3aprivate\r\n<\/pre>\n<p>\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u4e2a\u8bed\u53e5\u4f1a\u7f16\u8bd1\u51fa\u9519\u3002\u6839\u636eC++\u6807\u51c6\uff0c\u7b2c\u4e8c\u4e2a\u8bed\u53e5\u5e94\u8be5\u505a\u4ee5\u4e0b\u4e24\u4ef6\u4e8b\u60c5\uff1a\u7528&#8221;filename&#8221;\u521d\u59cb\u5316\u4e00\u4e2a\u4e34\u65f6\u7684ifstream\u5bf9\u8c61\uff0c\u628a\u4e34\u65f6\u7684\u5bf9\u8c61\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u590d\u5236\u7ed9file\u3002\u7531\u4e8e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662fprivate\uff0c\u6240\u4ee5\u6ca1\u6709\u6743\u9650\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u7f16\u8bd1\u51fa\u9519\u3002<\/p>\n<p>\u6ce8\uff1aVisual Studio\u7684C++\u7f16\u8bd1\u5668\u5e76\u6ca1\u6709\u9075\u5faa\u6807\u51c6\uff0c\u800c\u662f\u4e0d\u8003\u8651\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u5426\u4e3aprivate\uff0c\u76f4\u63a5\u7f16\u8bd1\u901a\u8fc7\u3002<\/p>\n<p><strong>2\u3001\u5f53\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e3aexplicit\uff0c\u6216\u8005\u6307\u5b9a\u7684\u6784\u9020\u51fd\u6570\u4e3aexplicit\u65f6\u3002<\/strong><\/p>\n<p>C++\u4e2d\u5982\u679c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4e3aexplicit\uff0c\u90a3\u4e48\u53ea\u80fd\u663e\u5f0f\u8c03\u7528\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u628a\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u7528\u4f5c\u201c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u201d\u662f\u4e0d\u53ef\u4ee5\u7684\u3002<\/p>\n<p>\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u7c7bA\uff0c\u7136\u540e\u8fdb\u884c\u4ee5\u4e0b\u6d4b\u8bd5<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nclass A{\r\npublic:\r\n    A(int a){}\r\n    A(const A &amp;a){}\r\n};\r\n\r\nint main(){\r\n    A a(1);\r\n    A b=1;\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002\u7136\u540e\u6211\u4eec\u628a\u53c2\u6570\u4e3aint\u7684\u6784\u9020\u51fd\u6570\u6539\u4e3aexplicit\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nclass A{\r\npublic:\r\n    explicit A(int a){}\r\n    A(const A &amp;a){}\r\n};\r\n\r\nint main(){\r\n    A a(1);\r\n    A b=1;\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u201cA a(1)\u201d\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\uff0c\u201cA b=1\u201d\u8fd9\u53e5\u62a5\u9519\uff0c\u9519\u8bef\u4fe1\u606f\u662f\u201cconversion from &#8216;int&#8217; to non-scalar type &#8216;A&#8217; requested\u201d\u3002\u8fd9\u610f\u5473\u7740\u7f16\u8bd1\u5668\u627e\u4e0d\u5230int\u7c7b\u578b\u8f6c\u6362\u6210A\u7c7b\u578b\u7684\u8f6c\u6362\u51fd\u6570\u4e86\u3002\u5982\u679c\u4e0d\u52a0explicit\uff0c\u53c2\u6570\u4e3aint\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u628aint\u8f6c\u6362\u4e3aA\u7c7b\u578b\uff0c\u4f46\u662f\u73b0\u5728\u4e0d\u53ef\u4ee5\u4e86\u3002\u7b2c\u4e00\u53e5\u7684\u201cA a(1)\u201d\u6709\u53c2\u6570\u4e3aint\u7684\u6784\u9020\u51fd\u6570\u7684\u663e\u5f0f\u8c03\u7528\uff0c\u800c\u7b2c\u4e8c\u53e5\u201cA b=1\u201d\u6ca1\u6709\u663e\u5f0f\u8c03\u7528\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u53ea\u628a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6539\u6210explicit<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nclass A{\r\npublic:\r\n    A(int a){}\r\n    explicit A(const A &amp;a){}\r\n};\r\n\r\nint main(){\r\n    A a(1);\r\n    A b=1;\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u8fd8\u662f\u7b2c\u4e8c\u53e5\u7f16\u8bd1\u9519\u8bef\u3002\u9519\u8bef\u4fe1\u606f\u662f\u201cno matching function for call to &#8216;A::A(A)&#8217;\u201d\uff0c\u8fd9\u8bf4\u660e\u7f16\u8bd1\u5668\u627e\u4e0d\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e86\u3002\u548c\u524d\u9762\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e2d\u8bf4\u660e\u7684\u4e00\u6837\uff0c\u7f16\u8bd1\u5668\u5728\u628a\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230b\u4e2d\u65f6\uff0c\u9700\u8981\u8c03\u7528A\u7c7b\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u73b0\u5728\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662fexplicit\u7684\uff0c\u53ea\u80fd\u663e\u5f0f\u7684\u8c03\u7528\uff0c\u4e0d\u80fd\u9690\u542b\u8c03\u7528\u3002\u7b2c\u4e8c\u53e5\u6ca1\u6709\u201cA b(temp)\u201d\u8fd9\u6837\u663e\u5f0f\u590d\u5236\u5bf9\u8c61\uff0c\u6240\u4ee5\u7f16\u8bd1\u51fa\u9519\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++\u4e2d\u7684\u76f4\u63a5\u521d\u59cb\u5316\u6307\u7684\u662f\u76f4\u63a5\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f8b\u5982 string a; \/\/\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 st &hellip; <a href=\"https:\/\/sqrt-1.me\/?p=241\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cC++\u7684\u76f4\u63a5\u521d\u59cb\u5316\u4e0e\u590d\u5236\u521d\u59cb\u5316\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[2],"tags":[],"class_list":["post-241","post","type-post","status-publish","format-standard","hentry","category-c-language"],"_links":{"self":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=241"}],"version-history":[{"count":6,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/241\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}