{"id":246,"date":"2021-06-07T18:36:07","date_gmt":"2021-06-07T10:36:07","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=246"},"modified":"2025-10-09T13:56:05","modified_gmt":"2025-10-09T05:56:05","slug":"cpp-standard-library-03-implicit-type-conversion","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/07\/cpp-standard-library-03-implicit-type-conversion\/","title":{"rendered":"C++\u6807\u51c6\u5e93 03 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"<p>&emsp;&emsp;\u300aC++ Primer\u300b\u4e2d\u63d0\u5230\uff1a<br \/>\n&emsp;&emsp;\u201c\u53ef\u4ee5\u7528<strong>\u5355\u4e2a\u5f62\u53c2\u6765\u8c03\u7528<\/strong>\u7684\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e86\u4ece<strong>\u5f62\u53c2\u7c7b\u578b<\/strong>\u5230<strong>\u8be5\u7c7b\u7c7b\u578b<\/strong>\u7684\u4e00\u4e2a\u9690\u5f0f\u8f6c\u6362\u3002\u201d<br \/>\n&emsp;&emsp;\u8fd9\u91cc\u5e94\u8be5\u6ce8\u610f\u7684\u662f\uff0c \u201c\u53ef\u4ee5\u7528\u5355\u4e2a\u5f62\u53c2\u8fdb\u884c\u8c03\u7528\u201d \u5e76\u4e0d\u662f\u6307\u6784\u9020\u51fd\u6570\u53ea\u80fd\u6709\u4e00\u4e2a\u5f62\u53c2\uff0c\u800c\u662f\u5b83\u53ef\u4ee5\u6709\u591a\u4e2a\u5f62\u53c2\uff0c\u4f46\u90a3\u4e9b\u5f62\u53c2\u90fd\u662f\u6709\u9ed8\u8ba4\u5b9e\u53c2\u7684\u3002<br \/>\n&emsp;&emsp;\u90a3\u4e48\uff0c\u4ec0\u4e48\u662f\u201c\u9690\u5f0f\u8f6c\u6362\u201d\u5462\uff1f \u4e0a\u9762\u8fd9\u53e5\u8bdd\u4e5f\u8bf4\u4e86\uff0c\u662f\u4ece \u6784\u9020\u51fd\u6570\u5f62\u53c2\u7c7b\u578b \u5230 \u8be5\u7c7b\u7c7b\u578b \u7684\u4e00\u4e2a\u7f16\u8bd1\u5668\u7684\u81ea\u52a8\u8f6c\u6362\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4ee3\u7801\u6765\u770b\u4e00\u770b:<\/p>\n<\/p>\n<pre><code class=\"language-cpp\">#include &lt;string&gt;\n#include &lt;iostream&gt;\nusing namespace std;\n\nclass BOOK  \/\/\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e66\u7c7b\n{\nprivate:\n    string _bookISBN;  \/\/\u4e66\u7684ISBN\u53f7\n    double _price;    \/\/\u4e66\u7684\u4ef7\u683c\n\npublic:\n    \/\/\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5373\u90a3\u4e2a\u201c\u80fd\u591f\u7528\u4e00\u4e2a\u53c2\u6570\u8fdb\u884c\u8c03\u7528\u7684\u6784\u9020\u51fd\u6570\u201d\uff08\u867d\u7136\u5b83\u6709\u4e24\u4e2a\u5f62\u53c2\uff0c\u4f46\u5176\u4e2d\u4e00\u4e2a\u6709\u9ed8\u8ba4\u5b9e\u53c2\uff0c\u53ea\u7528\u4e00\u4e2a\u53c2\u6570\u4e5f\u80fd\u8fdb\u884c\u8c03\u7528\uff09\n    BOOK(string ISBN, double price = 0.0f) :_bookISBN(ISBN), _price(price) { }\n\n    \/\/\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u5373\u662f\u90a3\u4e2a\u201c\u671f\u5f85\u4e00\u4e2a\u5b9e\u53c2\u4e3a\u7c7b\u7c7b\u578b\u7684\u51fd\u6570\u201d\n    \/\/\u8fd9\u4e2a\u51fd\u6570\u7528\u4e8e\u6bd4\u8f83\u4e24\u672c\u4e66\u7684ISBN\u53f7\u662f\u5426\u76f8\u540c\n    bool isSameISBN(const BOOK&amp; other)\n    {\n        return other._bookISBN == _bookISBN;\n    }\n};\n\nint main()\n{\n    BOOK A(&quot;A-A-A&quot;);\n    BOOK B(&quot;B-B-B&quot;);\n\n    cout &lt;&lt; A.isSameISBN(B) &lt;&lt; endl;   \/\/\u6b63\u7ecf\u5730\u8fdb\u884c\u6bd4\u8f83\uff0c\u65e0\u9700\u53d1\u751f\u8f6c\u6362\n\n    cout &lt;&lt; A.isSameISBN(string(&quot;A-A-A&quot;)) &lt;&lt; endl; \/\/\u6b64\u5904\u5373\u53d1\u751f\u4e00\u4e2a\u9690\u5f0f\u8f6c\u6362\uff1astring\u7c7b\u578b--&gt;BOOK\u7c7b\u578b\uff0c\u501f\u52a9BOOK\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u4ee5\u6ee1\u8db3isSameISBN\u51fd\u6570\u7684\u53c2\u6570\u671f\u5f85\u3002\n    cout &lt;&lt; A.isSameISBN(BOOK(&quot;A-A-A&quot;)) &lt;&lt; endl;    \/\/\u663e\u5f0f\u521b\u5efa\u4e34\u65f6\u5bf9\u8c61\uff0c\u4e5f\u5373\u662f\u7f16\u8bd1\u5668\u5e72\u7684\u4e8b\u60c5\u3002\n\n    return 0;\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0cisSameISBN\u51fd\u6570\u662f\u671f\u5f85\u4e00\u4e2aBOOK\u7c7b\u7c7b\u578b\u5f62\u53c2\u7684\uff0c\u4f46\u6211\u4eec\u5374\u4f20\u9012\u4e86\u4e00\u4e2astring\u7c7b\u578b\u7684\u7ed9\u5b83\uff0c\u8fd9\u4e0d\u662f\u5b83\u60f3\u8981\u7684\u554a\uff01\u8fd8\u597d\uff0cBOOK\u7c7b\u4e2d\u6709\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2astring\u7c7b\u578b\u5b9e\u53c2\u8fdb\u884c\u8c03\u7528\uff0c\u7f16\u8bd1\u5668\u8c03\u7528\u4e86\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u9690\u5f0f\u5730\u5c06stirng\u7c7b\u578b\u8f6c\u6362\u4e3aBOOK\u7c7b\u578b\uff08\u6784\u9020\u4e86\u4e00\u4e2aBOOK\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u518d\u4f20\u9012\u7ed9isSameISBN\u51fd\u6570\u3002<br \/>\n&emsp;&emsp;\u9690\u5f0f\u7c7b\u7c7b\u578b\u8f6c\u6362\u8fd8\u662f\u4f1a\u5e26\u6765\u98ce\u9669\u7684\uff0c\u6b63\u5982\u4e0a\u9762\u6807\u8bb0\uff0c\u9690\u5f0f\u8f6c\u6362\u5f97\u5230\u7c7b\u7684\u4e34\u65f6\u53d8\u91cf\uff0c\u5b8c\u6210\u64cd\u4f5c\u540e\u5c31\u6d88\u5931\u4e86\uff0c\u6211\u4eec\u6784\u9020\u4e86\u4e00\u4e2a\u5b8c\u6210\u6d4b\u8bd5\u540e\u88ab\u4e22\u5f03\u7684\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;\u300aC++ Primer\u300b\u4e2d\u63d0\u5230\uff1a &emsp;&emsp;\u201c\u53ef\u4ee5\u7528\u5355\u4e2a\u5f62\u53c2\u6765\u8c03\u7528\u7684\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e86\u4ece\u5f62\u53c2\u7c7b\u578b &#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":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/246"}],"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=246"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":436,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/246\/revisions\/436"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}