{"id":48,"date":"2007-08-14T23:40:15","date_gmt":"2007-08-15T03:40:15","guid":{"rendered":"http:\/\/prglab.com\/blog\/p\/48"},"modified":"2009-02-20T16:50:44","modified_gmt":"2009-02-20T20:50:44","slug":"difference-java-cpp","status":"publish","type":"post","link":"https:\/\/www.prglab.com\/blog\/p\/48","title":{"rendered":"\u4eceJava\u5230C++ &#8212; \u5bf9\u6bd4Java\u4e0eC++\u7f16\u7a0b\u7684\u4e0d\u540c"},"content":{"rendered":"<p>\u539f\u4f5c\uff1a<a href=\"http:\/\/www.horstmann.com\/\" target=\"_blank\">Cay Horstmann<\/a>      <a href=\"http:\/\/www.horstmann.com\/ccj2\/ccjapp3.html\" target=\"_blank\">\u82f1\u6587\u539f\u6587<\/a><\/p>\n<p>\u7ffb\u8bd1\uff1aAqua\u00a0 <a href=\"http:\/\/prglab.com\/blog\/p\/48\" title=\"\u5168\u6587\u94fe\u63a5\">prglab.com<\/a><\/p>\n<blockquote><p>\u6ce8\uff1a\u5f88\u591a\u7a0b\u5e8f\u5458\u5305\u62ec\u672c\u4eba\u5728\u5185\u90fd\u662f\u5148\u5b66\u4f1a\u7684Java\uff0c\u7136\u540e\u624d\u5b66\u7684C++\uff0c\u5176\u5b9eC++\u4e0eJava\u6709\u5f88\u591a\u76f8\u4f3c\u548c\u4e92\u901a\u4e4b\u5904\uff0c\u6709\u76f8\u5f53\u7684Java\u77e5\u8bc6\u53ef\u4ee5\u5bf9\u5e94\u8f6c\u5316\u5230C++\u6982\u5ff5\uff0c\u4ece\u800c\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u4e0a\u624b\u3002\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u5bf9\u4eceJava\u5411C++\u8f6c\u53d8\u5f88\u6709\u5e2e\u52a9\uff0c\u6240\u4ee5\u7ffb\u8bd1\u63a8\u8350\u7ed9\u6709\u540c\u6837\u9700\u8981\u7684\u670b\u53cb\u3002\u7ffb\u8bd1\u4e2d\u52a0\u5165\u4e86\u672c\u4eba\u7684\u7406\u89e3\uff0c\u4e0d\u5b8c\u5168\u662f\u5168\u6587\u7167\u642c\u3002\u6709\u4e0d\u660e\u6216\u5f02\u8bae\uff0c\u8bf7\u53c2\u8003\u539f\u6587\uff0c\u6216\u7559\u8a00\u8ba8\u8bba\u3002\u8fd8\u662f\u90a3\u53e5\u8bdd\uff0c\u63a8\u8350\u591a\u8bfb\u539f\u7248\u8d44\u6599\u3002<\/p><\/blockquote>\n<p>\u5b66\u4e60\u600e\u6837\u4ece\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u8f6c\u79fb\u5230\u53e6\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u662f\u4eca\u65e5\u7684\u7a0b\u5e8f\u5458\u5fc5\u987b\u9762\u5bf9\u7684\u73b0\u5b9e\uff0c\u8fd8\u597d\uff0cC++\u548cJava\u6709\u5f88\u591a\u5171\u540c\u7684\u7279\u70b9\uff0c\u6240\u4ee5\u4eceJava\u8f6c\u5230C++\u5c31\u5bb9\u6613\u8bb8\u591a\u3002C++\u6bd4Java\u590d\u6742\u5f88\u591a\uff0c\u672c\u6587\u5e76\u6ca1\u6253\u7b97\u6db5\u76d6\u6240\u6709C++\u7684\u529f\u80fd\u3002\u4f46\u662f\u5982\u679c\u4f60\u80fd\u591f\u719f\u7ec3\u638c\u63e1\u672c\u6587\u4e2d\u7684\u6240\u6709\u77e5\u8bc6\u70b9\uff0c\u4e5f\u8db3\u591f\u6709\u6548\u7684\u4f7f\u7528C++\u4e86\u3002<!--more--><\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u53ea\u8bb2Java\u4e0eC++\u7684\u4e0d\u540c\u4e4b\u5904\u3002\u50cf\u6d41\u7a0b\u63a7\u5236(if, while, for)\u8fd9\u4e9b\u5728C++\u4e0eJava\u4e2d\u5b8c\u5168\u4e00\u6837\u7684\u5185\u5bb9\u8fd9\u91cc\u5c31\u4e0d\u8bb2\u4e86\u3002<\/p>\n<p>\u672c\u6587\u662f\u57fa\u4e8eANSI C++\u6807\u51c6\u7684\uff0c\u4e00\u4e9b\u8001\u7684C++ \u7f16\u8bd1\u5668\u53ef\u80fd\u4e0d\u652f\u6301\u8fd9\u91cc\u8bb2\u5230\u7684\u4e00\u4e9b\u91cd\u8981\u529f\u80fd\u3002\u8981\u4f7f\u7528\u8fd9\u4e9b\u7f16\u8bd1\u5668\uff0c\u4f60\u9700\u8981\u66f4\u591a\u5b66\u4e60C++\u4e2d\u4eceC\u7ee7\u627f\u6765\u7684\u90e8\u5206\u3002\u90a3\u4e9b\u90fd\u8d85\u51fa\u4e86\u672c\u6587\u7684\u8303\u7574\uff0c\u4e5f\u5c31\u4e0d\u4ecb\u7ecd\u4e86\u3002<\/p>\n<h2>1. \u6570\u636e\u7c7b\u578b\u548c\u53d8\u91cf<\/h2>\n<p>C++ \u4e2d\u7684\u53d8\u91cf\u7c7b\u578b\u4e0eJava\u5f88\u76f8\u4f3c\u3002\u50cfJava\u4e00\u6837\uff0cC++ \u6709<tt>int<\/tt> \u548c <tt>double<\/tt> \u7c7b\u578b\u3002\u4f46\u662f\u8fd9\u4e9b\u6570\u5b57\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f\u4f9d\u8d56\u4e8e\u673a\u5668\u7684\u3002\u6bd4\u5982\u572816\u4f4d\u7cfb\u7edf\u4e0a\uff0c\u4f8b\u5982\u8fd0\u884cDOS \u6216Windows 3.x\u7684PC\u673a\u4e0a\uff0c<tt>int<\/tt> \u662f\u53cc\u5b57\u8282(2-byte)\u7684\uff0c\u53d6\u503c\u8303\u56f4\u6bd4Java\u76844-byte\u7684<tt>int<\/tt> \u8981\u5c0f\u5f88\u591a\u3002\u5728\u8fd9\u4e9b\u673a\u5668\u4e0a\uff0c\u5982\u679c int \u4e0d\u591f\u7528\u7684\u8bdd\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u957f\u6574\u578blong\u3002<\/p>\n<p>C++ \u6709 <tt>short<\/tt> \u548c <tt>unsigned<\/tt> \u7c7b\u578b\u6765\u66f4\u6709\u6548\u7684\u5b58\u50a8\u6570\u5b57\u3002\uff08\u6211\u8ba4\u4e3a\u6240\u8c13\u6709\u6548\u662f\u6307\u66f4\u9ad8\u7684\u7a7a\u95f4\u5229\u7528\u7387\u3002\uff09 \u6700\u597d\u662f\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u578b\u9664\u975e\u662f\u7a7a\u95f4\u5229\u7528\u7684\u6709\u6548\u6027\u5bf9\u4f60\u7684\u7cfb\u7edf\u771f\u7684\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p>\u5728C++\u4e2d\u5e03\u5c14\u578b\u7528 <tt>bool<\/tt> \u8868\u793a\uff0c\u800c\u4e0d\u50cf\u5728Java\u4e2d\u7528boolean\u3002<\/p>\n<p>C++ \u4e2d\u5b57\u7b26\u4e32\u7c7b\u578b\u7528 <tt>string<\/tt> \u8868\u793a\u3002\u5b83\u4e0eJava\u4e2d\u7684 <tt>String<\/tt> \u7c7b\u578b\u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u662f\uff0c\u8fd8\u662f\u8981\u9010\u4e00\u4ee5\u4e0b\u51e0\u70b9\u4e0d\u540c\u4e4b\u5904\uff1a<\/p>\n<p>1. C++ \u5b57\u7b26\u4e32\u5b58\u50a8ASCII \u7801\u5b57\u7b26\uff0c\u800c\u4e0d\u662f\u6807\u51c6\u7801Unicode \u5b57\u7b26<\/p>\n<p>2. C++ \u5b57\u7b26\u4e32\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\uff0c\u800cJava\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u662f\u4e0d\u53ef\u4fee\u6539\u7684(immutable)\u3002<\/p>\n<p>3. \u53d6\u5b50\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u5728 C++ \u4e2d\u53eb\u505a <tt>substr<\/tt>\uff0c\u8fd9\u4e2a\u547d\u4ee4<tt>s.substr(i, n)<\/tt> \u4ece\u5b57\u7b26\u4e32s\u4e2d\u53d6\u5f97\u4ece\u4f4d\u7f6e i \u5f00\u59cb\u957f\u5ea6\u4e3an\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p>4. \u5728C++\u4e2d\uff0c\u4f60\u53ea\u80fd\u591f\u5c06\u5b57\u7b26\u4e32\u4e0e\u5176\u5b83\u5b57\u7b26\u4e32\u5bf9\u8c61\u76f8\u4e32\u8054(concatenate)\uff0c\u800c\u4e0d\u80fd\u591f\u4e0e\u4efb\u610f\u7684\u5bf9\u8c61\u76f8\u4e32\u8054\u3002<\/p>\n<p>5. C++\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5173\u7cfb\u64cd\u4f5c\u7b26 <tt>==\u3001 !=\u3001 &lt;\u3001 &lt;=\u3001       &gt;\u3001 &gt;= <\/tt>\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u5176\u4e2d\u540e\u9762\u56db\u4e2a\u64cd\u4f5c\u7b26\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u8fdb\u884c\u6bd4\u8f83\u7684\u3002 \u8fd9\u6bd4Java\u4e2d\u4f7f\u7528\u51fd\u6570equals\u548ccompareTo\u6765\u6bd4\u8f83\u8981\u65b9\u4fbf\u5f88\u591a\u3002<\/p>\n<h2>2. \u53d8\u91cf\u548c\u5e38\u91cf<\/h2>\n<p>\u5728C++\u4e2d\uff0c\u672c\u5730\u53d8\u91cf\u7684\u5b9a\u4e49\u770b\u8d77\u6765\u4e0eJava\u4e2d\u76f8\u540c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>int n = 5;<\/tt><\/p>\n<p>\u5b9e\u9645\u4e0a\u8fd9\u6b63\u662fC++\u548cJava\u7684\u4e00\u4e2a\u91cd\u8981\u4e0d\u540c\u4e4b\u5904\u3002C++\u7f16\u8bd1\u5668\u4e0d\u5bf9\u672c\u5730\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u68c0\u9a8c\uff0c\u6240\u4ee5\u5728C++\u4e2d\u5f88\u5bb9\u6613\u5fd8\u8bb0\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u503c\u8be5\u53d8\u91cf\u6240\u5360\u5185\u5b58\u533a\u57df\u4e2d\u521a\u597d\u5f53\u524d\u5b58\u5728\u968f\u673a\u503c\u3002\u8fd9\u663e\u7136\u662f\u5f88\u5bb9\u6613\u4ea7\u751f\u7a0b\u5e8f\u51fa\u9519\u7684\u5730\u65b9\u3002<\/p>\n<p>\u4e0eJava\u4e00\u6837\uff0c C++\u4e2d\u7c7b\u53ef\u4ee5\u6709\u6570\u636e\u57df\u548c\u9759\u6001\u53d8\u91cf\u3002\u4e0d\u540c\u7684\u662f\uff0cC++\u4e2d\u53d8\u91cf\u53ef\u4ee5\u5728\u51fd\u6570\u751a\u81f3\u662f\u7c7b\u7684\u5916\u9762\u5b9a\u4e49\uff0c\u8fd9\u4e9b\u6240\u8c13\u7684\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u51fd\u6570\u4e2d\u88ab\u8bbf\u95ee\uff0c\u56e0\u800c\u4e0d\u6613\u88ab\u5f88\u597d\u7684\u7ba1\u7406\u3002\u6240C++\u4e2d\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<p>\u5728C++\u4e2d\uff0c\u5e38\u91cf\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u5b9a\u4e49\uff08\u8bb0\u5f97\u5728Java\u4e2d\uff0c\u5e38\u91cf\u5fc5\u987b\u662f\u7c7b\u7684\u9759\u6001\u6570\u636estatic data)\u3002 C++ \u4f7f\u7528\u5173\u952e\u5b57 <tt>const<\/tt> \u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u800cJava\u4e2d\u662f <tt>final<\/tt>\u3002\u4f8b\u5982\uff1a<\/p>\n<p><tt>const int DAYS_PER_YEAR = 365;<\/tt><\/p>\n<h2>3. \u7c7b<\/h2>\n<p>C++ \u4e2d\u5bf9\u7c7b\u7684\u5b9a\u4e49\u4e0eJava\u6709\u4e9b\u4e0d\u540c\uff0c\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a\u4e00\u4e2aC++ \u7248\u672c\u7684 <tt>Point<\/tt> \u7c7b:<\/p>\n<p><tt>class Point \/* C++ *\/<\/tt><\/p>\n<p><tt>{<\/tt><\/p>\n<p><tt>    public:<\/tt><\/p>\n<p><tt>        Point();<\/tt><\/p>\n<p><tt>        Point(double xval, double yval);<\/tt><\/p>\n<p><tt>        void move(double dx, double dy);<\/tt><\/p>\n<p><tt>        double getX() const;<\/tt><\/p>\n<p><tt>          double getY() const;<\/tt><\/p>\n<p><tt>    private:<\/tt><\/p>\n<p><tt>        double x;<\/tt><\/p>\n<p><tt>        double y;<\/tt><\/p>\n<p><tt>};<\/tt><\/p>\n<p>\u8fd9\u91cc\u51e0\u70b9\u91cd\u8981\u7684\u4e0d\u540c\u662f\uff1a<\/p>\n<p>1. C++\u7684\u7c7b\u5b9a\u4e49\u4e2d\u5206\u4e3a\u516c\u5171\u548c\u79c1\u6709\u90e8\u5206\uff0c\u5206\u522b\u4ee5\u5173\u952e\u5b57 <tt>public<\/tt> \u548c <tt>private<\/tt>\u5f00\u59cb\u3002\u800c\u5728Java\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u5fc5\u987b\u6807\u660e <tt>public<\/tt> \u6216 <tt>private<\/tt>\u3002<\/p>\n<p>2. C++\u4e2d\u7c7b\u7684\u5b9a\u4e49\u53ea\u5305\u542b\u51fd\u6570\u7684\u58f0\u660e\uff0c\u771f\u6b63\u7684\u5b9e\u73b0\u53e6\u5916\u5355\u72ec\u5217\u51fa\u3002<\/p>\n<p>3. \u8bbf\u95ee\u51fd\u6570(accessor methods)\u6807\u6709\u5173\u952e\u5b57 <tt>const<\/tt> \uff0c\u8868\u660e\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4f1a\u6539\u53d8\u672c\u5bf9\u8c61\u7684\u5143\u7d20\u503c\u3002<\/p>\n<p>4. \u7c7b\u5b9a\u4e49\u7684\u7ed3\u5c3e\u5904\u6709\u5206\u53f7<\/p>\n<p>\u7c7b\u4e2d\u51fd\u6570\u7684\u5b9e\u73b0\u8ddf\u5728\u7c7b\u7684\u5b9a\u4e49\u4e4b\u540e\u3002\u56e0\u4e3a\u51fd\u6570\u662f\u5728\u7c7b\u5916\u9762\u5b9a\u4e49\u7684\uff0c\u6240\u4ee5\u6bcf\u4e00\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u524d\u9762\u8981\u52a0\u7c7b\u540d\u79f0\u4f5c\u4e3a\u524d\u7f00\uff0c\u5e76\u4f7f\u7528\u64cd\u4f5c\u7b26\u53cc\u5192\u53f7::\u6765\u5206\u5272\u7c7b\u7684\u540d\u79f0\u548c\u51fd\u6570\u7684\u540d\u79f0\u3002\u4e0d\u6539\u53d8\u9690\u542b\u53c2\u6570\u503c\uff08\u5373\u5f53\u524d\u5bf9\u8c61\u7684\u503c\uff09\u7684\u8bbf\u95ee\u51fd\u6570\u7528       <tt>const<\/tt>\u6807\u660e\u3002\u5982\u4e0b\u6240\u793a\u662f\u4e0a\u9762\u7c7b\u5b9a\u4e49\u4e2d\u7684\u51fd\u6570\u7684\u5b9e\u73b0\uff1a<\/p>\n<p><tt>Point::Point() { x = 0; y = 0; }<\/tt><\/p>\n<p><tt>void Point::move(double dx, double dy) <\/tt><\/p>\n<p><tt>{<\/tt><\/p>\n<p><tt>  x = x + dx;<\/tt><\/p>\n<p><tt>  y = y + dy;<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p><tt>double Point::getX() const<\/tt><\/p>\n<p><tt>{  <\/tt><\/p>\n<p><tt>    return x;<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<h2>4. \u5bf9\u8c61<\/h2>\n<p>Java \u4e0e C++ \u6700\u4e3b\u8981\u7684\u4e0d\u540c\u5728\u4e8e\u5bf9\u8c61\u53d8\u91cf\u7684\u4f7f\u7528\u3002\u5728 C++\u4e2d\uff0c\u5bf9\u8c61\u53d8\u91cf\u5b58\u50a8\u7684\u662f\u771f\u6b63\u7684\u5bf9\u8c61\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u5f15\u7528(<a href=\"http:\/\/www.prglab.com\/blog\/p\/28\" title=\"\u5173\u4e8ereference\u7684\u6587\u7ae0\">reference<\/a>)\u3002\u6ce8\u610f\u5728C++\u4e2d\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\u662f\u4e0d\u4f7f\u7528\u5173\u952e\u5b57new\u7684\uff0c\u53ea\u9700\u8981\u5728\u53d8\u91cf\u7684\u540d\u5b57\u540e\u9762\u76f4\u63a5\u8d4b\u4e88\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>Point p(1, 2); \/* \u6784\u9020\u5bf9\u8c61 p *\/<\/tt><\/p>\n<p>\u5982\u679c\u4e0d\u8ddf\u53c2\u6570\u8d4b\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>Time now; \/* \u9ed8\u8ba4\u4f7f\u7528\u6784\u9020\u51fd\u6570 Time::Time() *\/<\/tt><\/p>\n<p>\u8fd9\u4e00\u70b9\u4e0eJava\u5f88\u4e0d\u540c\u3002\u5728Java\u4e2d\uff0c\u8fd9\u4e2a\u547d\u4ee4\u4ec5\u4ec5\u751f\u6210\u4e00\u4e2a\u6ca1\u6709\u521d\u59cb\u5316\u7684reference\uff0c\u800c\u5728C++\u4e2d\uff0c\u5b83\u751f\u6210\u4e00\u4e2a\u5b9e\u9645\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u8d4b\u7ed9\u53e6\u4e00\u4e2a\u5bf9\u8c61\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u5b9e\u9645\u7684\u503c\u5c06\u88ab\u62f7\u8d1d\u3002\u800c\u5728Java\u4e2d\uff0c\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u53d8\u91cf\u53ea\u4e0d\u8fc7\u662f\u5efa\u7acb\u4e86\u53e6\u5916\u4e00\u4e2a\u6307\u5411\u5bf9\u8c61\u7684reference\u3002\u62f7\u8d1d\u4e00\u4e2aC++\u7684\u5bf9\u8c61\u5c31\u50cf\u5728Java\u4e2d\u8c03\u7528clone\u8fd9\u4e2a\u51fd\u6570\u4e00\u6837\uff0c\u800c\u4fee\u6539\u62f7\u8d1d\u7684\u503c\u4e0d\u4f1a\u6539\u53d8\u539f\u5bf9\u8c61\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<p><tt>Point q = p; \/* \u62f7\u8d1dp\u5230q *\/<\/tt><\/p>\n<p><tt>q.move(1, 1); \/* \u79fb\u52a8q\u800cp\u4e0d\u52a8\uff0c\u5373q\u7684\u503c\u53d8\u4e86\uff0c\u800cp\u7684\u4e0d\u53d8*\/<\/tt><\/p>\n<p>\u591a\u6570\u60c5\u51b5\u4e0b\uff0cC++\u4e2d\u8fd9\u79cd\u5bf9\u8c61\u76f4\u63a5\u5bf9\u503c\u64cd\u4f5c\u7684\u7279\u6027\u4f7f\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\uff0c\u4f46\u662f\u4e5f\u6709\u4e9b\u65f6\u5019\u4e0d\u5c3d\u5982\u4eba\u610f\uff1a<\/p>\n<p>1. \u5f53\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u4e2d\u4fee\u6539\u4e00\u4e2a\u5bf9\u8c61\u7684\u503c\uff0c\u5fc5\u987b\u8bb0\u4f4f\u8981\u4f7f\u7528\u6309\u5f15\u7528\u8c03\u7528call       by reference (\u53c2\u89c1\u4e0b\u9762\u51fd\u6570\u90e8\u5206)<\/p>\n<p>2. \u4e24\u4e2a\u5bf9\u8c61\u53d8\u91cf\u4e0d\u80fd\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f53\u3002\u5982\u679c\u4f60\u8981\u5728C++\u4e2d\u5b9e\u73b0\u8fd9\u79cd\u6548\u679c\uff0c\u5fc5\u987b\u4f7f\u7528\u6307\u9488pointer\uff08\u53c2\u89c1\u4e0b\u9762\u6307\u9488\u90e8\u5206\uff09<\/p>\n<p>3. \u4e00\u4e2a\u5bf9\u8c61\u53d8\u91cf\u53ea\u80fd\u5b58\u50a8\u4e00\u79cd\u7279\u5b9a\u7684\u7c7b\u578b\u7684\u503c\uff0c\u5982\u679c\u4f60\u60f3\u8981\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u4e0d\u540c\u5b50\u7c7b\u7684\u5bf9\u8c61\u7684\u503c\uff08\u591a\u6001ploymorphism)\uff0c\u5219\u9700\u8981\u4f7f\u7528\u6307\u9488\u3002<\/p>\n<p>4. \u5982\u679c\u4f60\u60f3\u5728C++\u4e2d\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u6216\u8005\u6307\u5411null\u6216\u8005\u6307\u5411\u4e00\u4e2a\u5b9e\u9645\u7684\u5bf9\u8c61\uff0c\u5219\u9700\u8981\u4f7f\u7528\u6307\u9488<\/p>\n<h2>5. \u51fd\u6570<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u51fd\u6570\u5fc5\u987b\u6216\u8005\u662f\u5bf9\u8c61\u51fd\u6570(instance method)\uff0c\u6216\u8005\u662f\u9759\u6001\u51fd\u6570(static function)\u6216\u79f0\u7c7b\u51fd\u6570\u3002C++\u540c\u6837\u652f\u6301\u5bf9\u8c61\u51fd\u6570\u548c\u9759\u6001\u51fd\u6570\uff08\u7c7b\u51fd\u6570\uff09\uff0c\u4f46\u540c\u65f6C++\u4e5f\u5141\u8bb8\u5b9a\u4e49\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u7c7b\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u53eb\u505a\u5168\u5c40\u51fd\u6570<em>\uff08global functions\uff09<\/em>\u3002<\/p>\n<p>\u7279\u522b\u7684\u662f\uff0c\u6bcf\u4e00\u4e2aC++ \u7a0b\u5e8f\u90fd\u4ece\u4e00\u4e2a\u53eb\u505a <tt>main<\/tt>\u7684\u5168\u5c40\u51fd\u6570\u5f00\u59cb\u6267\u884c\uff1a<\/p>\n<p><tt>int main()<\/tt><\/p>\n<p><tt>{ . . .<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p>\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u683c\u5f0f\u7684main\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u6355\u6349\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u7c7b\u4f3c\u4e8eJava\u7684main\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u8981\u6c42\u5173\u4e8eC\u683c\u5f0f\u7684\u6570\u7ec4\u548c\u5b57\u7b26\u4e32\u7684\u77e5\u8bc6\uff0c\u8fd9\u91cc\u5c31\u4e0d\u4ecb\u7ecd\u4e86\u3002<\/p>\n<p>\u6309\u7167\u4e60\u60ef\uff0c\u901a\u5e38\u5982\u679c\u7a0b\u5e8f\u6267\u884c\u6210\u529f\uff0c <tt>main<\/tt> \u51fd\u6570\u8fd4\u56de0\uff0c\u5426\u5219\u8fd4\u56de\u975e\u96f6\u6574\u6570\u3002<\/p>\n<p>\u540cJava\u4e00\u6837\uff0c\u51fd\u6570\u53c2\u6570\u662f\u901a\u8fc7\u503c\u4f20\u9012\u7684(passed by value)\u3002\u5728Java\u4e2d\uff0c\u51fd\u6570\u65e0\u8bba\u5982\u4f55\u90fd\u662f\u53ef\u4ee5\u4fee\u6539\u5bf9\u8c61\u7684\u503c\u7684\u3002\u7136\u800c\u5728C++\u4e2d\uff0c\u56e0\u4e3a\u5bf9\u8c61\u76f4\u63a5\u5b58\u50a8\u7684\u662f\u5b9e\u9645\u7684\u503c\uff0c\u800c\u4e0d\u662f\u6307\u5411\u503c\u7684reference\uff0c\u4e5f\u5c31\u662f\u8bf4\u4f20\u5165\u51fd\u6570\u7684\u662f\u4e00\u4e2a\u5b9e\u9645\u503c\u7684\u62f7\u8d1d\uff0c\u56e0\u6b64\u4e5f\u5c31\u65e0\u6cd5\u4fee\u6539\u539f\u6765\u5bf9\u8c61\u7684\u503c\u3002<\/p>\n<p>\u6240\u4ee5\uff0cC++ \u6709\u4e24\u79cd\u53c2\u6570\u4f20\u9012\u673a\u5236\uff0c\u540cJava\u4e00\u6837\u7684\u6309\u503c\u8c03\u7528(<em>call by value) <\/em>\uff0c\u4ee5\u53ca\u6309\u5730\u5740\u8c03\u7528(<em>call by reference<\/em>)\u3002\u5f53\u4e00\u4e2a\u53c2\u6570\u662f\u6309reference\u4f20\u9012\u65f6\uff0c\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u5176\u539f\u59cb\u503c\u3002Call by reference \u7684\u53c2\u6570\u524d\u9762\u6709\u4e00\u4e2a\u5730\u5740\u53f7 <tt>&amp;<\/tt> \u8ddf\u5728\u53c2\u6570\u7c7b\u578b\u7684\u540e\u9762\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>void raiseSalary(Employee&amp; e, double by)<\/tt><\/p>\n<p><tt>{ . . .<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5229\u7528call by reference\u7684\u51fd\u6570\uff0c\u5728Java\u4e2d\u662f\u65e0\u6cd5\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\u7684\u3002<\/p>\n<p><tt>void swap(int&amp; a, int&amp; b)<\/tt><\/p>\n<p><tt>{  int temp = a;<\/tt><\/p>\n<p><tt>  a = b;<\/tt><\/p>\n<p><tt>  b = temp;<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p>\u5982\u679c\u4f7f\u7528 <tt>swap(x, y)<\/tt>\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5219reference\u53c2\u6570 <tt>a<\/tt> \u548c <tt>b<\/tt> \u6307\u5411\u539f\u5b9e\u9645\u53c2\u6570<tt>x<\/tt> \u548c <tt>y<\/tt>\u7684\u4f4d\u7f6e\uff0c\u800c\u4e0d\u662f\u5b83\u4eec\u7684\u503c\u7684\u62f7\u8d1d\uff0c\u56e0\u6b64\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u9645\u4ea4\u6362\u8fd9\u4e24\u4e2a\u53c2\u6570\u7684\u503c\u3002<\/p>\n<p>\u5728 C++\u4e2d\uff0c\u6bcf\u5f53\u9700\u8981\u5b9e\u73b0\u4fee\u6539\u539f\u53c2\u6570\u7684\u503c\u65f6\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u6309\u5730\u5740\u8c03\u7528 call by reference \u3002<\/p>\n<h2>6. \u5411\u91cfVector<\/h2>\n<p>C++ \u7684\u5411\u91cf\u7ed3\u6784\u7ed3\u5408\u4e86Java\u4e2d\u6570\u7ec4\u548c\u5411\u91cf\u4e24\u8005\u7684\u4f18\u70b9\u3002\u4e00\u4e2aC++ \u7684\u5411\u91cf\u53ef\u4ee5\u65b9\u4fbf\u7684\u88ab\u8bbf\u95ee\uff0c\u5176\u5bb9\u91cf\u53c8\u53ef\u4ee5\u52a8\u6001\u7684\u589e\u957f\u3002\u5982\u679c <tt>T<\/tt> \u662f\u4efb\u610f\u7c7b\u578b\uff0c\u5219 <tt>vector&lt;T&gt;<\/tt>       \u662f\u4e00\u4e2a\u5143\u7d20\u4e3a <tt>T<\/tt> \u7c7b\u578b\u7684\u52a8\u6001\u6570\u7ec4\u3002\u4e0b\u9762\u7684\u8bed\u53e5<\/p>\n<p><tt>vector&lt;int&gt; a;<\/tt><\/p>\n<p>\u4ea7\u751f\u4e00\u4e2a\u521d\u59cb\u4e3a\u7a7a\u7684\u5411\u91cf\u3002\u800c\u8bed\u53e5<\/p>\n<p><tt>vector&lt;int&gt; a(100);<\/tt><\/p>\n<p>\u751f\u6210\u4e00\u4e2a\u521d\u59cb\u6709100\u4e2a\u5143\u7d20\u7684\u5411\u91cf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<tt>push_back<\/tt> \u51fd\u6570\u6765\u6dfb\u52a0\u5143\u7d20\uff1a<\/p>\n<p><tt>a.push_back(n);<\/tt><\/p>\n<p>\u8c03\u7528 <tt>a.pop_back()<\/tt> \u4ece<tt>a<\/tt>\u4e2d\u53d6\u51fa\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff08\u64cd\u4f5c\u540e\u8fd9\u4e2a\u5143\u7d20\u88ab\u4ecea\u4e2d\u5220\u6389)\uff0c       \u4f7f\u7528\u51fd\u6570<tt>size<\/tt> \u53ef\u4ee5\u5f97\u5230\u5f53\u524da\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6211\u4eec\u719f\u6089\u7684 <tt>[]<\/tt> \u64cd\u4f5c\u7b26\u6765\u8bbf\u95ee\u5411\u91cf\u4e2d\u5143\u7d20\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>for (i = 0; i &lt; a.size(); i++) {<br \/>\n<\/tt><\/p>\n<p><tt>  sum = sum + a[i];<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p>\u540cJava\u4e2d\u4e00\u6837\uff0c\u6570\u7ec4\u7d22\u5f15\u5fc5\u987b\u4e3a 0 \u548c <tt>a.size() - 1<\/tt>\u4e4b\u95f4\u7684\u503c\u3002\u4f46\u662f\u4e0eJava\u4e0d\u540c\u7684\u662f\uff0cC++\u4e2d\u6ca1\u6709runtime\u7684\u7d22\u5f15\u53f7\u5408\u6cd5\u6027\u68c0\u9a8c\u3002\u8bd5\u56fe\u8bbf\u95ee\u975e\u6cd5\u7684\u7d22\u5f15\u4f4d\u7f6e\u53ef\u80fd\u9020\u6210\u975e\u5e38\u4e25\u91cd\u7684\u51fa\u9519\u3002<\/p>\n<p>\u5c31\u50cf\u6240\u6709\u5176\u5b83 C++ \u5bf9\u8c61\u4e00\u6837\uff0c\u5411\u91cf\u4e5f\u662f\u503c\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u5411\u91cf\u8d4b\u503c\u7ed9\u53e6\u5916\u4e00\u4e2a\u5411\u91cf\u53d8\u91cf\uff0c\u6240\u6709\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u62f7\u8d1d\u8fc7\u53bb\u3002<\/p>\n<p><tt>vector&lt;int&gt; b = a; \/* \u6240\u6709\u7684\u5143\u7d20\u90fd\u88ab\u62f7\u8d1d\u4e86 *\/<\/tt><\/p>\n<p>\u5bf9\u6bd4Java\u4e2d\u7684\u60c5\u51b5\uff0c\u5728Java\u4e2d\uff0c\u4e00\u4e2a\u6570\u7ec4\u53d8\u91cf\u662f\u4e00\u4e2a\u6307\u5411\u6570\u7ec4\u7684reference\u3002\u62f7\u8d1d\u8fd9\u4e2a\u53d8\u91cf\u4ec5\u4ec5\u4ea7\u751f\u53e6\u5916\u4e00\u4e2a\u6307\u5411\u540c\u4e00\u6570\u7ec4\u7684reference\uff0c\u800c\u4e0d\u4f1a\u62f7\u8d1d\u6bcf\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u3002<\/p>\n<p>\u6b63\u56e0\u5982\u6b64\uff0c\u5982\u679c\u4e00\u4e2aC++\u51fd\u6570\u8981\u5b9e\u73b0\u4fee\u6539\u5411\u91cf\u7684\u503c\uff0c\u5fc5\u987b\u4f7f\u7528reference\u53c2\u6570\uff1a<\/p>\n<p><tt>void sort(vector&lt;int&gt;&amp; a)<\/tt><\/p>\n<p><tt>{ . . .<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<h2>7. \u8f93\u5165\u548c\u8f93\u51fa<\/h2>\n<p>\u5728C++\u4e2d\uff0c\u6807\u51c6\u7684\u8f93\u5165\u8f93\u51fa\u6d41\u7528\u5bf9\u8c61       <tt>cin<\/tt> \u548c <tt>cout<\/tt> \u8868\u793a\u3002\u6211\u4eec\u4f7f\u7528 <tt>&lt;&lt;<\/tt>       \u64cd\u4f5c\u7b26\u5199\u8f93\u51fa\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>cout &lt;&lt; \"Hello, World!\";<\/tt><\/p>\n<p>\u4e5f\u53ef\u4ee5\u8fde\u7740\u8f93\u51fa\u591a\u9879\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>cout &lt;&lt; \"The answer is \" &lt;&lt; x &lt;&lt; \"\\n\";<\/tt><\/p>\n<p>\u6211\u4eec\u4f7f\u7528 <tt>&gt;&gt;<\/tt>       \u64cd\u4f5c\u7b26\u6765\u8bfb\u5165\u4e00\u4e2a\u6570\u5b57\u6216\u5355\u8bcd\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>double x;<\/tt><\/p>\n<p><tt>cout &lt;&lt; \"Please enter x: \";<\/tt><\/p>\n<p><tt>cin &gt;&gt; x;<\/tt><\/p>\n<p><tt>string fname;<\/tt><\/p>\n<p><tt>cout &lt;&lt; \"Please enter your first name: \";<\/tt><\/p>\n<p><tt>cin &gt;&gt; fname;<\/tt><\/p>\n<p>\u51fd\u6570<tt>getline<\/tt> \u53ef\u4ee5\u8bfb\u5165\u6574\u884c\u7684\u8f93\u5165\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>string inputLine;<\/tt><\/p>\n<p><tt>getline(cin, inputLine);<\/tt><\/p>\n<p>\u5982\u679c\u5230\u8fbe\u8f93\u5165\u7684\u7ed3\u5c3e\uff0c\u6216\u8005\u4e00\u4e2a\u6570\u5b57\u65e0\u6cd5\u88ab\u6b63\u786e\u7684\u8bfb\u5165\uff0c\u8fd9\u4e2a\u6d41\u5bf9\u8c61\u4f1a\u88ab\u8bbe\u7f6e\u4e3a failed \u72b6\u6001\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570 <tt>fail<\/tt> \u6765\u68c0\u9a8c\u8fd9\u4e2a\u72b6\u6001\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>int n;<\/tt><\/p>\n<p><tt>cin &gt;&gt; n;<\/tt><\/p>\n<p><tt>if (cin.fail()) cout &lt;&lt; \"Bad input\";<\/tt><\/p>\n<p>\u4e00\u65e6\u4e00\u4e2a\u6d41\u7684\u72b6\u6001\u88ab\u8bbe\u4e3afailed\uff0c\u6211\u4eec\u662f\u5f88\u96be\u91cd\u7f6e\u5b83\u7684\u72b6\u6001\u7684\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u9519\u8bef\u8f93\u5165\u7684\u60c5\u51b5\uff0c\u5e94\u8be5\u4f7f\u7528\u51fd\u6570 <tt>getline<\/tt> \u7136\u540e\u4eba\u5de5\u5904\u7406\u5f97\u5230\u7684\u8f93\u5165\u6570\u636e\u3002<\/p>\n<h2>8. \u6307\u9488pointer<\/h2>\n<p>\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u5728C++\u4e2d\uff0c\u5bf9\u8c61\u53d8\u91cf\u76f4\u63a5\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\u7684\u503c\u3002\u8fd9\u662f\u4e0eJava\u4e0d\u540c\u7684\uff0c\u5728Java\u4e2d\u5bf9\u8c61\u53d8\u91cf\u5b58\u50a8\u7684\u662f\u4e00\u4e2a\u5730\u5740\uff0c\u8be5\u5730\u5740\u6307\u5411\u5bf9\u8c61\u503c\u5b9e\u9645\u5b58\u50a8\u7684\u5730\u65b9\u3002\u6709\u65f6\u5728C++\u4e2d\u4e5f\u9700\u8981\u5b9e\u73b0\u8fd9\u6837\u7684\u5e03\u7f6e\uff0c\u8fd9\u5c31\u7528\u5230\u4e86\u6307\u9488pointer\u3002\u5728 C++\u4e2d\uff0c\u4e00\u4e2a\u6307\u5411\u5bf9\u8c61\u7684\u53d8\u91cf\u53eb\u505a\u6307\u9488\u3002\u5982\u679cT\u662f\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5219 <tt>T*<\/tt> \u662f\u6307\u5411\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\u7684\u6307\u9488\u3002 \u8fd9\u91cc\u91cd\u70b9\u4ecb\u7ecdC++\u4e0eJava\u7684\u4e0d\u540c\uff0c\u8981\u8be6\u7ec6\u4e86\u89e3C++\u4e2d\u6307\u9488\u7684\u4f7f\u7528\uff0c\u8bf7\u53c2\u8003\u672c\u7ad9\u53e6\u4e00\u7bc7\u6587\u7ae0\u201c<strong><a href=\"http:\/\/www.prglab.com\/blog\/p\/28\" rel=\"bookmark\" title=\"Permanent Link to C++\u4e2dReference\u4e0e\u6307\u9488\uff08Pointer\uff09\u7684\u4f7f\u7528\u5bf9\u6bd4\">C++\u4e2dReference\u4e0e\u6307\u9488\uff08Pointer\uff09\u7684\u4f7f\u7528\u5bf9\u6bd4<\/a><\/strong>\u201d\u3002<\/p>\n<p>\u5c31\u50cf Java\u4e2d\u4e00\u6837\uff0c\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u53ef\u4ee5\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\u503c <tt>NULL<\/tt>\uff0c\u53e6\u5916\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u6216\u8005\u4e00\u4e2a\u8c03\u7528new\u751f\u6210\u7684\u65b0\u5bf9\u8c61\uff1a<\/p>\n<p><tt>Employee* p = NULL;<\/tt><\/p>\n<p><tt>Employee* q = new Employee(\"Hacker, Harry\", 35000);<\/tt><\/p>\n<p><tt>Employee* r = q;<\/tt><\/p>\n<p>\u5b9e\u9645\u4e0a\u5728C++\u4e2d\u8fd8\u6709\u7b2c\u56db\u79cd\u53ef\u80fd\uff0c\u90a3\u5c31\u662f\u6307\u9488\u53ef\u4ee5\u88ab\u521d\u59cb\u5316\u4e3a\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u8fd9\u9700\u8981\u4f7f\u7528\u5730\u5740\u64cd\u4f5c\u7b26 <tt>&amp;<\/tt> \uff1a<\/p>\n<p><tt>Employee boss(\"Morris, Melinda\", 83000);<\/tt><\/p>\n<p><tt>Employee* s = &amp;boss;<\/tt><\/p>\n<p>\u8fd9\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u4ec0\u4e48\u597d\u4e3b\u610f\u3002\u4fdd\u9669\u7684\u505a\u6cd5\u8fd8\u662f\u5e94\u8be5\u76f4\u63a5\u8ba9\u6307\u9488\u6307\u5411\u4f7f\u7528 <tt>new<\/tt>\u751f\u6210\u7684\u65b0\u5bf9\u8c61\u3002<\/p>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0cC++ \u6307\u9488\u770b\u8d77\u6765\u975e\u5e38\u50cf Java \u7684\u5bf9\u8c61\u53d8\u91cf\u3002\u7136\u800c\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u8bed\u6cd5\u7684\u4e0d\u540c\u3002\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u661f\u53f7\u64cd\u4f5c\u7b26 <tt>*<\/tt> \u6765\u8bbf\u95ee\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u3002\u5982\u679c <tt>p<\/tt> \u662f\u4e00\u4e2a\u6307\u5411<tt>Employee<\/tt>\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5219 <tt>*p<\/tt> \u624d\u4ee3\u8868\u4e86\u8fd9\u4e2a\u5bf9\u8c61\uff1a<\/p>\n<p><tt>Employee* p = . . .;<\/tt><\/p>\n<p><tt>Employee boss = <strong>*p<\/strong>;<\/tt><\/p>\n<p>\u5f53\u6211\u4eec\u9700\u8981\u6267\u884c\u5bf9\u8c61\u7684\u51fd\u6570\u6216\u8bbf\u95ee\u5bf9\u8c61\u7684\u4e00\u4e2a\u6570\u636e\u57df\u65f6\uff0c\u4e5f\u9700\u8981\u4f7f\u7528 <tt>*p<\/tt> \uff1a<\/p>\n<p><tt>(*p).setSalary(91000);<\/tt><\/p>\n<p>*p\u5916\u9762\u7684\u62ec\u53f7\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a <tt>.<\/tt> \u64cd\u4f5c\u7b26\u6bd4 * \u64cd\u4f5c\u7b26\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\u3002C\u7684\u8bbe\u8ba1\u8005\u89c9\u5f97\u8fd9\u79cd\u5199\u6cd5\u5f88\u96be\u770b\uff0c\u6240\u4ee5\u4ed6\u4eec\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u66ff\u4ee3\u7684\u5199\u6cd5\uff0c\u4f7f\u7528 -&gt; \u64cd\u4f5c\u7b26\u6765\u5b9e\u73b0 <tt>*<\/tt> \u548c <tt>.<\/tt> \u64cd\u4f5c\u7b26\u7684\u7ec4\u5408\u529f\u80fd\u3002\u8868\u8fbe\u5f0f<\/p>\n<p><tt>p-&gt;setSalary(91000);<\/tt><\/p>\n<p>\u53ef\u4ee5\u8c03\u7528\u5bf9\u8c61*p\u7684\u51fd\u6570 <tt>setSalary<\/tt> \u3002\u4f60\u53ef\u4ee5\u7b80\u5355\u7684\u8bb0\u4f4f <tt>.<\/tt> \u64cd\u4f5c\u7b26\u662f\u5728\u5bf9\u8c61\u4e0a\u4f7f\u7528\u7684\uff0c-&gt; \u64cd\u4f5c\u7b26\u662f\u5728\u6307\u9488\u4e0a\u4f7f\u7528\u7684\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e0d\u521d\u59cb\u5316\u4e00\u4e2a\u6307\u9488\uff0c\u6216\u8005\u5982\u679c\u4e00\u4e2a\u6307\u9488\u4e3a\u7a7a\u503c NULL \u6216\u6307\u5411\u7684\u5bf9\u8c61\u4e0d\u518d\u5b58\u5728\uff0c\u5219\u5728\u5b83\u4e0a\u9762\u4f7f\u7528 <tt>*<\/tt>       \u6216 <tt>-&gt;<\/tt> \u64cd\u4f5c\u7b26\u5c31\u4f1a\u51fa\u9519\u3002 \u4e0d\u5e78\u7684\u662f C++ runtime \u7cfb\u7edf\u5e76\u4e0d\u68c0\u67e5\u8fd9\u4e2a\u51fa\u9519\u3002\u5982\u679c\u4f60\u8303\u4e86\u8fd9\u4e2a\u9519\u8bef\uff0c\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u884c\u4e3a\u53e4\u602a\u6216\u6b7b\u673a\u3002<\/p>\n<p>\u800c\u5728Java\u4e2d\uff0c\u8fd9\u4e9b\u9519\u8bef\u662f\u4e0d\u4f1a\u53d1\u751f\u7684\u3002\u6240\u6709\u7684reference\u90fd\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u53ea\u8981\u4ecd\u6709reference\u6307\u5411\u5b83\u5c31\u4e0d\u4f1a\u88ab\u4ece\u5185\u5b58\u4e2d\u6e05\u9664\uff0c\u56e0\u6b64\u4f60\u4e5f\u4e0d\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u5df2\u88ab\u5220\u9664\u7684\u5bf9\u8c61\u7684reference\u3002Java\u7684runtime      \u7cfb\u7edf\u4f1a\u68c0\u67e5reference\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u5728\u9047\u5230\u7a7a\u6307\u9488\u65f6\u629b\u51fa\u4e00\u4e2anull pointer\u7684\u4f8b\u5916(exception)\u3002<\/p>\n<p>C++ \u548c Java\u8fd8\u6709\u4e00\u4e2a\u663e\u8457\u7684\u4e0d\u540c\uff0c\u5c31\u662f Java \u6709<em>\u5783\u573e\u56de\u6536<\/em>\u529f\u80fd\uff0c\u80fd\u591f\u81ea\u52a8\u56de\u6536\u88ab\u5e9f\u5f03\u7684\u5bf9\u8c61\u3002\u800c\u5728C++\u4e2d\uff0c\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\u5206\u914d\u56de\u6536\u3002<\/p>\n<p>C++\u4e2d\u5f53\u5bf9\u8c61\u53d8\u91cf\u8d85\u51fa\u8303\u56f4\u65f6\u53ef\u4ee5\u81ea\u52a8\u88ab\u56de\u6536\u3002\u4f46\u662f\u4f7f\u7528new\u751f\u6210\u7684\u5bf9\u8c61\u5fc5\u987b\u7528delete\u64cd\u4f5c\u7b26\u624b\u52a8\u5220\u9664\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>Employee* p = new Employee(\"Hacker, Harry\", 38000);<\/tt><\/p>\n<p><tt>. . .<\/tt><\/p>\n<p><tt>delete p; \/* \u4e0d\u5728\u9700\u8981\u8fd9\u4e2a\u5bf9\u8c61 *\/<\/tt><\/p>\n<p>\u5982\u679c\u4f60\u5fd8\u8bb0\u5220\u9664\u4e00\u4e2a\u5bf9\u8c61\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u6709\u53ef\u80fd\u6700\u7ec8\u7528\u5149\u6240\u6709\u5185\u5b58\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u5185\u5b58\u6cc4\u6f0f (<em>memory leak<\/em>)\u3002\u66f4\u91cd\u8981\u7684\u662f\uff0c\u5982\u679c\u4f60\u5982\u679c\u5220\u9664\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u53c8\u7ee7\u7eed\u4f7f\u7528\u5b83\uff0c\u4f60\u53ef\u80fd\u8986\u76d6\u4e0d\u5c5e\u4e8e\u4f60\u7684\u6570\u636e\u3002\u5982\u679c\u4f60\u521a\u5de7\u8986\u76d6\u4e86\u7528\u4e8e\u5904\u7406\u5185\u5b58\u56de\u6536\u7684\u6570\u636e\u57df\uff0c\u90a3\u4e48\u5185\u5b58\u5206\u914d\u673a\u5236\u5c31\u53ef\u80fd\u8fd0\u8f6c\u5931\u5e38\u800c\u9020\u6210\u66f4\u4e25\u91cd\u7684\u9519\u8bef\uff0c\u800c\u4e14\u5f88\u96be\u8bca\u65ad\u548c\u4fee\u590d\u3002\u56e0\u6b64\uff0c\u5728C++\u4e2d\u6700\u597d\u5c3d\u91cf\u5c11\u7528\u6307\u9488\u3002<\/p>\n<h2>9. \u7ee7\u627f<\/h2>\n<p>C++\u548cJava\u4e2d\u7ee7\u627f\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u5f88\u76f8\u4f3c\u7684\u3002\u5728C++\u4e2d\uff0c\u4f7f\u7528 <tt>: public<\/tt> \u4ee3\u66ffJava\u4e2d\u7684<tt>extends<\/tt> \u6765\u8868\u793a\u7ee7\u627f\u5173\u7cfb \u3002 (C++ \u4e5f\u652f\u6301\u79c1\u6709\u7ee7\u627f\u7684\u6982\u5ff5\uff0c\u4f46\u662f\u4e0d\u592a\u6709\u7528\u3002)<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++\u4e2d\u7684\u51fd\u6570\u4e0d\u662f\u52a8\u6001\u7ed1\u5b9a\u7684\u3002\u5982\u679c\u4f60\u9700\u8981\u67d0\u4e2a\u51fd\u6570\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a\uff0c\u9700\u8981\u4f7f\u7528<tt>virtual<\/tt>\u58f0\u660e\u5b83\u4e3a\u865a\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>class Manager : public Employee<\/tt><\/p>\n<p><tt>{ <\/tt><\/p>\n<p><tt>public:<\/tt><\/p>\n<p><tt>   Manager(string name, double salary, string dept);<\/tt><\/p>\n<p><tt>   <strong>virtual<\/strong> void print() const;<\/tt><\/p>\n<p><tt>private:<\/tt><\/p>\n<p><tt>   string department;<\/tt><\/p>\n<p><tt>};<\/tt><\/p>\n<p>\u540cJava\u4e00\u6837\uff0c\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6709\u7279\u6b8a\u7684\u8bed\u6cd5\u3002 Java\u4f7f\u7528\u5173\u952e\u5b57 <tt>super<\/tt>\u3002C++\u4e2d\u5fc5\u987b\u5728\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4f53\u5916\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p><tt>Manager::Manager(string name, double salary, string dept)<\/tt><\/p>\n<p><tt><strong>: Employee(name, salary) <\/strong>\/* \u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570 *\/<\/tt><\/p>\n<p><tt>{  department = dept;<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p>Java \u4e2d\u5728\u5b50\u7c7b\u51fd\u6570\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u51fd\u6570\u65f6\u4e5f\u4f7f\u7528\u5173\u952e\u5b57 <tt>super<\/tt> \u3002\u800c\u5728C++\u4e2d\u662f\u4f7f\u7528\u7236\u7c7b\u7684\u540d\u79f0\u52a0\u4e0a\u64cd\u4f5c\u7b26       <tt>::<\/tt>\u8868\u793a\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>void Manager::print() const<\/tt><\/p>\n<p><tt>{  <strong>Employee::print(); <\/strong>\/* \u8c03\u7528\u7236\u7c7b\u7684\u51fd\u6570       *\/<\/tt><\/p>\n<p><tt>   cout &lt;&lt; department &lt;&lt; \"\\n\";<\/tt><\/p>\n<p><tt>}<\/tt><\/p>\n<p>\u4e00\u4e2a C++ \u5bf9\u8c61\u53d8\u91cf\u53ea\u80fd\u5b58\u50a8\u7279\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u503c\u3002\u8981\u60f3\u5728C++\u4e2d\u5b9e\u73b0\u591a\u6001(polymorphism)\uff0c\u5fc5\u987b\u4f7f\u7528\u6307\u9488\u3002\u4e00\u4e2a <tt>T*<\/tt> \u6307\u9488\u53ef\u4ee5\u6307\u5411\u7c7b\u578b\u4e3a <tt>T<\/tt> \u6216 <tt>T<\/tt> \u7684\u4efb\u610f\u5b50\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><tt>Employee* e = new Manager(\"Morris, Melinda\", 83000, \"Finance\");<\/tt><\/p>\n<p>\u4f60\u53ef\u4ee5\u5c06\u7236\u7c7b\u548c\u4e0d\u540c\u5b50\u7c7b\u7684\u5bf9\u8c61\u6df7\u5408\u6536\u96c6\u5230\u4e00\u4e2a\u5143\u7d20\u5747\u4e3a\u6307\u9488\u7684\u5411\u91cf\u4e2d\uff0c\u7136\u540e\u8c03\u7528\u52a8\u6001\u7ed1\u5b9a\u7684\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><tt>vector&lt;Employee*&gt; staff;<\/tt><\/p>\n<p><tt>. . .<\/tt><\/p>\n<p><tt>for (i = 0; i &lt; staff.size(); i++)<\/tt><\/p>\n<p><tt>   staff[i]-&gt;print();<\/tt><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u4f5c\uff1aCay Horstmann \u82f1\u6587\u539f\u6587 \u7ffb\u8bd1\uff1aAqua\u00a0 prglab.com \u6ce8\uff1a\u5f88\u591a\u7a0b\u5e8f\u5458\u5305\u62ec\u672c\u4eba\u5728\u5185\u90fd\u662f\u5148\u5b66\u4f1a\u7684Java\uff0c\u7136\u540e\u624d\u5b66\u7684C++\uff0c\u5176\u5b9eC++\u4e0eJava\u6709\u5f88\u591a\u76f8\u4f3c\u548c\u4e92\u901a\u4e4b\u5904\uff0c\u6709\u76f8\u5f53\u7684Java\u77e5\u8bc6\u53ef\u4ee5\u5bf9\u5e94\u8f6c\u5316\u5230C++\u6982\u5ff5\uff0c\u4ece\u800c\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u4e0a\u624b\u3002\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u5bf9\u4eceJava\u5411C++\u8f6c\u53d8\u5f88\u6709\u5e2e\u52a9\uff0c\u6240\u4ee5\u7ffb\u8bd1\u63a8\u8350\u7ed9\u6709\u540c\u6837\u9700\u8981\u7684\u670b\u53cb\u3002\u7ffb\u8bd1\u4e2d\u52a0\u5165\u4e86\u672c\u4eba\u7684\u7406\u89e3\uff0c\u4e0d\u5b8c\u5168\u662f\u5168\u6587\u7167\u642c\u3002\u6709\u4e0d\u660e\u6216\u5f02\u8bae\uff0c\u8bf7\u53c2\u8003\u539f\u6587\uff0c\u6216\u7559\u8a00\u8ba8\u8bba\u3002\u8fd8\u662f\u90a3\u53e5\u8bdd\uff0c\u63a8\u8350\u591a\u8bfb\u539f\u7248\u8d44\u6599\u3002 \u5b66\u4e60\u600e\u6837\u4ece\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u8f6c\u79fb\u5230\u53e6\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u662f\u4eca\u65e5\u7684\u7a0b\u5e8f\u5458\u5fc5\u987b\u9762\u5bf9\u7684\u73b0\u5b9e\uff0c\u8fd8\u597d\uff0cC++\u548cJava\u6709\u5f88\u591a\u5171\u540c\u7684\u7279\u70b9\uff0c\u6240\u4ee5\u4eceJava\u8f6c\u5230C++\u5c31\u5bb9\u6613\u8bb8\u591a\u3002C++\u6bd4Java\u590d\u6742\u5f88\u591a\uff0c\u672c\u6587\u5e76\u6ca1\u6253\u7b97\u6db5\u76d6\u6240\u6709C++\u7684\u529f\u80fd\u3002\u4f46\u662f\u5982\u679c\u4f60\u80fd\u591f\u719f\u7ec3\u638c\u63e1\u672c\u6587\u4e2d\u7684\u6240\u6709\u77e5\u8bc6\u70b9\uff0c\u4e5f\u8db3\u591f\u6709\u6548\u7684\u4f7f\u7528C++\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}