锘??xml version="1.0" encoding="utf-8" standalone="yes"?>91精品久久久久久久久久另类,男女羞羞在线观看,视频一区免费在线观看http://www.aygfsteel.com/fhtdy2004/category/39265.html緇忛獙嫻呰杽錛岃€愬績縐瘡錛涜鎬т笉濂斤紝璁拌鏂囧瓧zh-cnSat, 31 Oct 2009 19:26:35 GMTSat, 31 Oct 2009 19:26:35 GMT60char str[]涓巆har *str涓€涓皬tiphttp://www.aygfsteel.com/fhtdy2004/archive/2009/08/05/289968.htmlFrank_FangFrank_FangWed, 05 Aug 2009 08:57:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/08/05/289968.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/289968.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/08/05/289968.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/289968.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/289968.html     const char str[] = "abc";
    char * s = const_cast<char *>(str);
    s[0] = 'A';
 
    cout<<s<<endl;    //杈撳嚭Abc
浣嗘槸榪欐牱灝變笉琛屼簡錛?
   const char *str = "abc";
    char * s = const_cast<char *>(str);
    s[0] = 'A';
 
    cout<<s<<endl;    //緙栬瘧鑳介€氳繃錛屼絾鏄墽琛屽氨鎶ラ敊銆?br />
: const char str[] = "abc";        abc瀛樺偍鍦ㄥ爢鏍堜腑錛?/font>
: const char *str = "abc";         abc瀛樺偍鍦ㄩ潤鎬佸瓨鍌ㄥ尯錛?/font>
鍑嗙‘鐨勮錛屼笂闈袱涓?#8220;abc"閮芥槸瀛樺偍鍦ㄩ潤鎬佸瓨鍌ㄥ尯錛屽嵆甯擱噺鍖恒€傚父閲忓尯鏄彲璇諱笉鍙啓鐨勩€傛墍浠ヤ換浣曡瘯鍥懼甯擱噺鍖鴻繘琛屽啓鐨勬搷浣滈兘鏄潪娉曠殑錛屽綋鐒朵簡錛岃繖涔熶笉鏄竴瀹氫笉鍙啓鐨勶紝浣犲彲浠ラ噰鍙栨煇縐嶆笭閬撴敼鍙樺父閲忓尯鐨勫唴瀛樺睘鎬э紝姣斿鏀瑰彉pe鐩稿叧鑺傜殑灞炴€у氨鍙互瀵瑰父閲忓尯榪涜璇誨啓錛屽綋鐒朵簡錛岃繖涓洰鍓嶅彲浠ュ拷鐣ャ€傘€傘€?
閭d箞涓轟粈涔坰tr[] = "abc";        鍙互鍐欏憿錛熺瓟妗堝氨鍦╯tr[] = "abc";浼氭湁涓€涓澶栫殑鎷瘋礉榪囩▼錛屽嵆鎶婂父閲忓尯鐨?"abc"鎷瘋礉鍒版爤鍐呭瓨鍘伙紝鎵€浠ュ氨鍙互鍐欎簡銆?


]]>
C++寮曠敤灝忕粨錛堣漿 錛?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/06/07/280417.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Sun, 07 Jun 2009 04:23:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/06/07/280417.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/280417.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/06/07/280417.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/280417.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/280417.html</trackback:ping><description><![CDATA[     鎽樿: 浠嬬粛C++寮曠敤鐨勫熀鏈蹇碉紝閫氳繃璇︾粏鐨勫簲鐢ㄥ垎鏋愪笌璇存槑錛屽寮曠敤榪涜鍏ㄩ潰銆侀€忓交鍦伴槓榪?  <a href='http://www.aygfsteel.com/fhtdy2004/archive/2009/06/07/280417.html'>闃呰鍏ㄦ枃</a><img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/280417.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-06-07 12:23 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/06/07/280417.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>C++涓皢鏋勯€犲嚱鏁版垨鏋愭瀯鍑芥暟瀹氫箟涓簆rivatehttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278971.htmlFrank_FangFrank_FangFri, 29 May 2009 16:22:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278971.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/278971.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278971.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/278971.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/278971.html闃呰鍏ㄦ枃

]]>
C++鐨剆tatic鍏抽敭瀛?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278968.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Fri, 29 May 2009 16:06:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278968.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/278968.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278968.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/278968.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/278968.html</trackback:ping><description><![CDATA[<p>銆€銆€C++鐨剆tatic鏈変袱縐嶇敤娉曪細闈㈠悜榪囩▼紼嬪簭璁捐涓殑static鍜岄潰鍚戝璞$▼搴忚璁′腑鐨剆tatic銆傚墠鑰呭簲鐢ㄤ簬鏅€氬彉閲忓拰鍑芥暟錛屼笉娑夊強綾伙紱鍚庤€呬富瑕佽鏄巗tatic鍦ㄧ被涓殑浣滅敤銆?/p> <p><strong>涓€銆侀潰鍚戣繃紼嬭璁′腑鐨剆tatic</strong></p> <p>1銆侀潤鎬佸叏灞€鍙橀噺</p> <p>鍦ㄥ叏灞€鍙橀噺鍓嶏紝鍔犱笂鍏抽敭瀛梥tatic錛岃鍙橀噺灝辮瀹氫箟鎴愪負涓€涓潤鎬佸叏灞€鍙橀噺銆傛垜浠厛涓句竴涓潤鎬佸叏灞€鍙橀噺鐨勪緥瀛愶紝濡備笅錛?</p> <pre>//Example 1 #include <iostream.h> void fn(); static int n; //瀹氫箟闈欐€佸叏灞€鍙橀噺 void main() { n=20; cout<<n<<endl; fn(); } void fn() { n++; cout<<n<<endl; } </pre> 闈欐€佸叏灞€鍙橀噺鏈変互涓嬬壒鐐癸細 <ul> <li>璇ュ彉閲忓湪鍏ㄥ眬鏁版嵁鍖哄垎閰嶅唴瀛橈紱 <li>鏈粡鍒濆鍖栫殑闈欐€佸叏灞€鍙橀噺浼氳紼嬪簭鑷姩鍒濆鍖栦負0錛堣嚜鍔ㄥ彉閲忕殑鍊兼槸闅忔満鐨勶紝闄ら潪瀹冭鏄懼紡鍒濆鍖栵級錛? <li>闈欐€佸叏灞€鍙橀噺鍦ㄥ0鏄庡畠鐨勬暣涓枃浠墮兘鏄彲瑙佺殑錛岃€屽湪鏂囦歡涔嬪鏄笉鍙鐨勶紱銆€ </li> </ul> 闈欐€佸彉閲忛兘鍦ㄥ叏灞€鏁版嵁鍖哄垎閰嶅唴瀛橈紝鍖呮嫭鍚庨潰灝嗚鎻愬埌鐨勯潤鎬佸眬閮ㄥ彉閲忋€傚浜庝竴涓畬鏁寸殑紼嬪簭錛屽湪鍐呭瓨涓殑鍒嗗竷鎯呭喌濡備笅鍥撅細<br /> 銆€ <table id="table1" width="10%" border="1"> <tbody> <tr> <td>浠g爜鍖?/td> </tr> <tr> <td>鍏ㄥ眬鏁版嵁鍖?/td> </tr> <tr> <td>鍫嗗尯</td> </tr> <tr> <td>鏍堝尯</td> </tr> </tbody> </table> <p>銆€銆€涓€鑸▼搴忕殑鐢眓ew浜х敓鐨勫姩鎬佹暟鎹瓨鏀懼湪鍫嗗尯錛屽嚱鏁板唴閮ㄧ殑鑷姩鍙橀噺瀛樻斁鍦ㄦ爤鍖恒€傝嚜鍔ㄥ彉閲忎竴鑸細闅忕潃鍑芥暟鐨勯€€鍑鴻€岄噴鏀劇┖闂達紝闈欐€佹暟鎹紙鍗充嬌鏄嚱鏁板唴閮ㄧ殑闈欐€佸眬閮ㄥ彉閲忥級涔熷瓨鏀懼湪鍏ㄥ眬鏁版嵁鍖恒€傚叏灞€鏁版嵁鍖虹殑鏁版嵁騫朵笉浼氬洜涓哄嚱鏁扮殑閫€鍑鴻€岄噴鏀劇┖闂淬€傜粏蹇冪殑璇昏€呭彲鑳戒細鍙戠幇錛孍xample 1涓殑浠g爜涓皢 </p> <pre> static int n; //瀹氫箟闈欐€佸叏灞€鍙橀噺</pre> 鏀逛負 <pre> int n; //瀹氫箟鍏ㄥ眬鍙橀噺</pre> 紼嬪簭鐓ф牱姝e父榪愯銆?br /> 鐨勭‘錛屽畾涔夊叏灞€鍙橀噺灝卞彲浠ュ疄鐜板彉閲忓湪鏂囦歡涓殑鍏變韓錛屼絾瀹氫箟闈欐€佸叏灞€鍙橀噺榪樻湁浠ヤ笅濂藉錛? <ul> <li>闈欐€佸叏灞€鍙橀噺涓嶈兘琚叾瀹冩枃浠舵墍鐢紱 <li>鍏跺畠鏂囦歡涓彲浠ュ畾涔夌浉鍚屽悕瀛楃殑鍙橀噺錛屼笉浼氬彂鐢熷啿紿侊紱 </li> </ul> <p>鎮ㄥ彲浠ュ皢涓婅堪紺轟緥浠g爜鏀逛負濡備笅錛?br /> </p> <pre>//Example 2 //File1 #include <iostream.h> void fn(); static int n; //瀹氫箟闈欐€佸叏灞€鍙橀噺 void main() { n=20; cout<<n<<endl; fn(); } //File2 #include <iostream.h> extern int n; void fn() { n++; cout<<n<<endl; } </pre> 緙栬瘧騫惰繍琛孍xample 2錛屾偍灝變細鍙戠幇涓婅堪浠g爜鍙互鍒嗗埆閫氳繃緙栬瘧錛屼絾榪愯鏃跺嚭鐜伴敊璇€傝瘯鐫€灝? <pre>static int n; //瀹氫箟闈欐€佸叏灞€鍙橀噺 </pre> 鏀逛負 <pre>int n; //瀹氫箟鍏ㄥ眬鍙橀噺 </pre> 鍐嶆緙栬瘧榪愯紼嬪簭錛岀粏蹇冧綋浼氬叏灞€鍙橀噺鍜岄潤鎬佸叏灞€鍙橀噺鐨勫尯鍒€? <p>2銆侀潤鎬佸眬閮ㄥ彉閲?/p> <p>鍦ㄥ眬閮ㄥ彉閲忓墠錛屽姞涓婂叧閿瓧static錛岃鍙橀噺灝辮瀹氫箟鎴愪負涓€涓潤鎬佸眬閮ㄥ彉閲忋€?</p> <p>鎴戜滑鍏堜婦涓€涓潤鎬佸眬閮ㄥ彉閲忕殑渚嬪瓙錛屽涓嬶細 </p> <pre>//Example 3 #include <iostream.h> void fn(); void main() { fn(); fn(); fn(); } void fn() { static n=10; cout<<n<<endl; n++; } </pre> 銆€銆€閫氬父錛屽湪鍑芥暟浣撳唴瀹氫箟浜嗕竴涓彉閲忥紝姣忓綋紼嬪簭榪愯鍒拌璇彞鏃墮兘浼氱粰璇ュ眬閮ㄥ彉閲忓垎閰嶆爤鍐呭瓨銆備絾闅忕潃紼嬪簭閫€鍑哄嚱鏁頒綋錛岀郴緇熷氨浼氭敹鍥炴爤鍐呭瓨錛屽眬閮ㄥ彉閲忎篃鐩稿簲澶辨晥銆?br /> 銆€銆€浣嗘湁鏃跺€欐垜浠渶瑕佸湪涓ゆ璋冪敤涔嬮棿瀵瑰彉閲忕殑鍊艱繘琛屼繚瀛樸€傞€氬父鐨勬兂娉曟槸瀹氫箟涓€涓叏灞€鍙橀噺鏉ュ疄鐜般€備絾榪欐牱涓€鏉ワ紝鍙橀噺宸茬粡涓嶅啀灞炰簬鍑芥暟鏈韓浜嗭紝涓嶅啀浠呭彈鍑芥暟鐨勬帶鍒訛紝緇欑▼搴忕殑緇存姢甯︽潵涓嶄究銆?br /> 銆€銆€闈欐€佸眬閮ㄥ彉閲忔濂藉彲浠ヨВ鍐寵繖涓棶棰樸€傞潤鎬佸眬閮ㄥ彉閲忎繚瀛樺湪鍏ㄥ眬鏁版嵁鍖猴紝鑰屼笉鏄繚瀛樺湪鏍堜腑錛屾瘡嬈$殑鍊間繚鎸佸埌涓嬩竴嬈¤皟鐢紝鐩村埌涓嬫璧嬫柊鍊箋€? <p>闈欐€佸眬閮ㄥ彉閲忔湁浠ヤ笅鐗圭偣錛?/p> <ul> <li>璇ュ彉閲忓湪鍏ㄥ眬鏁版嵁鍖哄垎閰嶅唴瀛橈紱 <li>闈欐€佸眬閮ㄥ彉閲忓湪紼嬪簭鎵ц鍒拌瀵硅薄鐨勫0鏄庡鏃惰棣栨鍒濆鍖栵紝鍗充互鍚庣殑鍑芥暟璋冪敤涓嶅啀榪涜鍒濆鍖栵紱 <li>闈欐€佸眬閮ㄥ彉閲忎竴鑸湪澹版槑澶勫垵濮嬪寲錛屽鏋滄病鏈夋樉寮忓垵濮嬪寲錛屼細琚▼搴忚嚜鍔ㄥ垵濮嬪寲涓?錛? <li>瀹冨緇堥┗鐣欏湪鍏ㄥ眬鏁版嵁鍖猴紝鐩村埌紼嬪簭榪愯緇撴潫銆備絾鍏朵綔鐢ㄥ煙涓哄眬閮ㄤ綔鐢ㄥ煙錛屽綋瀹氫箟瀹冪殑鍑芥暟鎴栬鍙ュ潡緇撴潫鏃訛紝鍏朵綔鐢ㄥ煙闅忎箣緇撴潫錛?</li> </ul> <p>3銆侀潤鎬佸嚱鏁?/p> <p>銆€銆€鍦ㄥ嚱鏁扮殑榪斿洖綾誨瀷鍓嶅姞涓妔tatic鍏抽敭瀛?鍑芥暟鍗寵瀹氫箟涓洪潤鎬佸嚱鏁般€傞潤鎬佸嚱鏁頒笌鏅€氬嚱鏁頒笉鍚岋紝瀹冨彧鑳藉湪澹版槑瀹冪殑鏂囦歡褰撲腑鍙錛屼笉鑳借鍏跺畠鏂囦歡浣跨敤銆?/p> <p>闈欐€佸嚱鏁扮殑渚嬪瓙錛?</p> <pre>//Example 4 #include <iostream.h> static void fn();//澹版槑闈欐€佸嚱鏁? void main() { fn(); } void fn()//瀹氫箟闈欐€佸嚱鏁? { int n=10; cout<<n<<endl; } </pre> 瀹氫箟闈欐€佸嚱鏁扮殑濂藉錛? <ul> <li>闈欐€佸嚱鏁頒笉鑳借鍏跺畠鏂囦歡鎵€鐢紱 <li>鍏跺畠鏂囦歡涓彲浠ュ畾涔夌浉鍚屽悕瀛楃殑鍑芥暟錛屼笉浼氬彂鐢熷啿紿侊紱 </li> </ul> <p><strong>浜屻€侀潰鍚戝璞$殑static鍏抽敭瀛楋紙綾諱腑鐨剆tatic鍏抽敭瀛楋級</strong></p> <p>1銆侀潤鎬佹暟鎹垚鍛?/p> <p>鍦ㄧ被鍐呮暟鎹垚鍛樼殑澹版槑鍓嶅姞涓婂叧閿瓧static錛岃鏁版嵁鎴愬憳灝辨槸綾誨唴鐨勯潤鎬佹暟鎹垚鍛樸€傚厛涓句竴涓潤鎬佹暟鎹垚鍛樼殑渚嬪瓙銆?</p> <pre>//Example 5 #include <iostream.h> class Myclass { public: Myclass(int a,int b,int c); void GetSum(); private: int a,b,c; static int Sum;//澹版槑闈欐€佹暟鎹垚鍛? }; int Myclass::Sum=0;//瀹氫箟騫跺垵濮嬪寲闈欐€佹暟鎹垚鍛? Myclass::Myclass(int a,int b,int c) { this->a=a; this->b=b; this->c=c; Sum+=a+b+c; } void Myclass::GetSum() { cout<<"Sum="<<Sum<<endl; } void main() { Myclass M(1,2,3); M.GetSum(); Myclass N(4,5,6); N.GetSum(); M.GetSum(); } </pre> 鍙互鐪嬪嚭錛岄潤鎬佹暟鎹垚鍛樻湁浠ヤ笅鐗圭偣錛? <ul> <li>瀵逛簬闈為潤鎬佹暟鎹垚鍛橈紝姣忎釜綾誨璞¢兘鏈夎嚜宸辯殑鎷瘋礉銆傝€岄潤鎬佹暟鎹垚鍛樿褰撲綔鏄被鐨勬垚鍛樸€傛棤璁鴻繖涓被鐨勫璞¤瀹氫箟浜嗗灝戜釜錛岄潤鎬佹暟鎹垚鍛樺湪紼嬪簭涓篃鍙湁涓€浠芥嫹璐濓紝鐢辮綾誨瀷鐨勬墍鏈夊璞″叡浜闂€備篃灝辨槸璇達紝闈欐€佹暟鎹垚鍛樻槸璇ョ被鐨勬墍鏈夊璞℃墍鍏辨湁鐨勩€傚璇ョ被鐨勫涓璞℃潵璇達紝闈欐€佹暟鎹垚鍛樺彧鍒嗛厤涓€嬈″唴瀛橈紝渚涙墍鏈夊璞″叡鐢ㄣ€傛墍浠ワ紝闈欐€佹暟鎹垚鍛樼殑鍊煎姣忎釜瀵硅薄閮芥槸涓€鏍風殑錛屽畠鐨勫€煎彲浠ユ洿鏂幫紱 <li>闈欐€佹暟鎹垚鍛樺瓨鍌ㄥ湪鍏ㄥ眬鏁版嵁鍖恒€傞潤鎬佹暟鎹垚鍛樺畾涔夋椂瑕佸垎閰嶇┖闂達紝鎵€浠ヤ笉鑳藉湪綾誨0鏄庝腑瀹氫箟銆傚湪Example 5涓紝璇彞int Myclass::Sum=0;鏄畾涔夐潤鎬佹暟鎹垚鍛橈紱 <li>闈欐€佹暟鎹垚鍛樺拰鏅€氭暟鎹垚鍛樹竴鏍烽伒浠巔ublic,protected,private璁塊棶瑙勫垯錛? <li>鍥犱負闈欐€佹暟鎹垚鍛樺湪鍏ㄥ眬鏁版嵁鍖哄垎閰嶅唴瀛橈紝灞炰簬鏈被鐨勬墍鏈夊璞″叡浜紝鎵€浠ワ紝瀹冧笉灞炰簬鐗瑰畾鐨勭被瀵硅薄錛屽湪娌℃湁浜х敓綾誨璞℃椂鍏朵綔鐢ㄥ煙灝卞彲瑙侊紝鍗沖湪娌℃湁浜х敓綾葷殑瀹炰緥鏃訛紝鎴戜滑灝卞彲浠ユ搷浣滃畠錛? <li>闈欐€佹暟鎹垚鍛樺垵濮嬪寲涓庝竴鑸暟鎹垚鍛樺垵濮嬪寲涓嶅悓銆傞潤鎬佹暟鎹垚鍛樺垵濮嬪寲鐨勬牸寮忎負錛?br /> 錛滄暟鎹被鍨嬶紴錛滅被鍚嶏紴::錛滈潤鎬佹暟鎹垚鍛樺悕錛?錛滃€鹼紴 <li>綾葷殑闈欐€佹暟鎹垚鍛樻湁涓ょ璁塊棶褰㈠紡錛?br /> 錛滅被瀵硅薄鍚嶏紴.錛滈潤鎬佹暟鎹垚鍛樺悕錛?鎴?錛滅被綾誨瀷鍚嶏紴::錛滈潤鎬佹暟鎹垚鍛樺悕錛?br /> 濡傛灉闈欐€佹暟鎹垚鍛樼殑璁塊棶鏉冮檺鍏佽鐨勮瘽錛堝嵆public鐨勬垚鍛橈級錛屽彲鍦ㄧ▼搴忎腑錛屾寜涓婅堪鏍煎紡鏉ュ紩鐢ㄩ潤鎬佹暟鎹垚鍛?錛? <li>闈欐€佹暟鎹垚鍛樹富瑕佺敤鍦ㄥ悇涓璞¢兘鏈夌浉鍚岀殑鏌愰」灞炴€х殑鏃跺€欍€傛瘮濡傚浜庝竴涓瓨嬈劇被錛屾瘡涓疄渚嬬殑鍒╂伅閮芥槸鐩稿悓鐨勩€傛墍浠ワ紝搴旇鎶婂埄鎭涓哄瓨嬈劇被鐨勯潤鎬佹暟鎹垚鍛樸€傝繖鏈変袱涓ソ澶勶紝絎竴錛屼笉綆″畾涔夊灝戜釜瀛樻綾誨璞★紝鍒╂伅鏁版嵁鎴愬憳閮藉叡浜垎閰嶅湪鍏ㄥ眬鏁版嵁鍖虹殑鍐呭瓨錛屾墍浠ヨ妭鐪佸瓨鍌ㄧ┖闂淬€傜浜岋紝涓€鏃﹀埄鎭渶瑕佹敼鍙樻椂錛屽彧瑕佹敼鍙樹竴嬈★紝鍒欐墍鏈夊瓨嬈劇被瀵硅薄鐨勫埄鎭叏鏀瑰彉榪囨潵浜嗭紱 <li>鍚屽叏灞€鍙橀噺鐩告瘮錛屼嬌鐢ㄩ潤鎬佹暟鎹垚鍛樻湁涓や釜浼樺娍錛?</li> </ul> <ol> <li>闈欐€佹暟鎹垚鍛樻病鏈夎繘鍏ョ▼搴忕殑鍏ㄥ眬鍚嶅瓧絀洪棿錛屽洜姝や笉瀛樺湪涓庣▼搴忎腑鍏跺畠鍏ㄥ眬鍚嶅瓧鍐茬獊鐨勫彲鑳芥€э紱 <li>鍙互瀹炵幇淇℃伅闅愯棌銆傞潤鎬佹暟鎹垚鍛樺彲浠ユ槸private鎴愬憳錛岃€屽叏灞€鍙橀噺涓嶈兘錛?br /> </li> </ol> 2銆侀潤鎬佹垚鍛樺嚱鏁? <p>銆€銆€涓庨潤鎬佹暟鎹垚鍛樹竴鏍鳳紝鎴戜滑涔熷彲浠ュ垱寤轟竴涓潤鎬佹垚鍛樺嚱鏁幫紝瀹冧負綾葷殑鍏ㄩ儴鏈嶅姟鑰屼笉鏄負鏌愪竴涓被鐨勫叿浣撳璞℃湇鍔°€傞潤鎬佹垚鍛樺嚱鏁頒笌闈欐€佹暟鎹垚鍛樹竴鏍鳳紝閮芥槸綾葷殑鍐呴儴瀹炵幇錛屽睘浜庣被瀹氫箟鐨勪竴閮ㄥ垎銆傛櫘閫氱殑鎴愬憳鍑芥暟涓€鑸兘闅愬惈浜嗕竴涓猼his鎸囬拡錛宼his鎸囬拡鎸囧悜綾葷殑瀵硅薄鏈韓錛屽洜涓烘櫘閫氭垚鍛樺嚱鏁版€繪槸鍏蜂綋鐨勫睘浜庢煇涓被鐨勫叿浣撳璞$殑銆傞€氬父鎯呭喌涓嬶紝this鏄己鐪佺殑銆傚鍑芥暟fn()瀹為檯涓婃槸this->fn()銆備絾鏄笌鏅€氬嚱鏁扮浉姣旓紝闈欐€佹垚鍛樺嚱鏁扮敱浜庝笉鏄笌浠諱綍鐨勫璞$浉鑱旂郴錛屽洜姝ゅ畠涓嶅叿鏈塼his鎸囬拡銆備粠榪欎釜鎰忎箟涓婅錛屽畠鏃犳硶璁塊棶灞炰簬綾誨璞$殑闈為潤鎬佹暟鎹垚鍛橈紝涔熸棤娉曡闂潪闈欐€佹垚鍛樺嚱鏁幫紝瀹冨彧鑳借皟鐢ㄥ叾浣欑殑闈欐€佹垚鍛樺嚱鏁般€備笅闈婦涓潤鎬佹垚鍛樺嚱鏁扮殑渚嬪瓙銆?</p> <pre>//Example 6 #include <iostream.h> class Myclass { public: Myclass(int a,int b,int c); static void GetSum();/澹版槑闈欐€佹垚鍛樺嚱鏁? private: int a,b,c; static int Sum;//澹版槑闈欐€佹暟鎹垚鍛? }; int Myclass::Sum=0;//瀹氫箟騫跺垵濮嬪寲闈欐€佹暟鎹垚鍛? Myclass::Myclass(int a,int b,int c) { this->a=a; this->b=b; this->c=c; Sum+=a+b+c; //闈為潤鎬佹垚鍛樺嚱鏁板彲浠ヨ闂潤鎬佹暟鎹垚鍛? } void Myclass::GetSum() //闈欐€佹垚鍛樺嚱鏁扮殑瀹炵幇 { // cout<<a<<endl; //閿欒浠g爜錛宎鏄潪闈欐€佹暟鎹垚鍛? cout<<"Sum="<<Sum<<endl; } void main() { Myclass M(1,2,3); M.GetSum(); Myclass N(4,5,6); N.GetSum(); Myclass::GetSum(); } </pre> <p> </p> <p> </p> 鍏充簬闈欐€佹垚鍛樺嚱鏁幫紝鍙互鎬葷粨涓轟互涓嬪嚑鐐癸細 <p> </p> <ul> <li>鍑虹幇鍦ㄧ被浣撳鐨勫嚱鏁板畾涔変笉鑳芥寚瀹氬叧閿瓧static錛? <li>闈欐€佹垚鍛樹箣闂村彲浠ョ浉浜掕闂紝鍖呮嫭闈欐€佹垚鍛樺嚱鏁拌闂潤鎬佹暟鎹垚鍛樺拰璁塊棶闈欐€佹垚鍛樺嚱鏁幫紱 <li>闈為潤鎬佹垚鍛樺嚱鏁板彲浠ヤ換鎰忓湴璁塊棶闈欐€佹垚鍛樺嚱鏁板拰闈欐€佹暟鎹垚鍛橈紱 <li>闈欐€佹垚鍛樺嚱鏁頒笉鑳借闂潪闈欐€佹垚鍛樺嚱鏁板拰闈為潤鎬佹暟鎹垚鍛橈紱 <li>鐢變簬娌℃湁this鎸囬拡鐨勯澶栧紑閿€錛屽洜姝ら潤鎬佹垚鍛樺嚱鏁頒笌綾葷殑鍏ㄥ眬鍑芥暟鐩告瘮閫熷害涓婁細鏈夊皯璁哥殑澧為暱錛? <li>璋冪敤闈欐€佹垚鍛樺嚱鏁幫紝鍙互鐢ㄦ垚鍛樿闂搷浣滅(.)鍜?->)涓轟竴涓被鐨勫璞℃垨鎸囧悜綾誨璞$殑鎸囬拡璋冪敤闈欐€佹垚鍛樺嚱鏁幫紝涔熷彲浠ョ洿鎺ヤ嬌鐢ㄥ涓嬫牸寮忥細<br /> 錛滅被鍚嶏紴::錛滈潤鎬佹垚鍛樺嚱鏁板悕錛烇紙錛滃弬鏁拌〃錛烇級<br /> 璋冪敤綾葷殑闈欐€佹垚鍛樺嚱鏁般€?</li> </ul> <img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/278968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-30 00:06 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/05/30/278968.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>static鍏ㄥ眬鍙橀噺涓庢櫘閫氱殑鍏ㄥ眬鍙橀噺鏈変粈涔堝尯鍒紵static灞€閮ㄥ彉閲忓拰鏅€氬眬閮ㄥ彉閲忔湁浠€涔堝尯鍒紵static鍑芥暟涓庢櫘閫氬嚱鏁版湁浠€涔堝尯鍒紵http://www.aygfsteel.com/fhtdy2004/archive/2009/05/29/278958.htmlFrank_FangFrank_FangFri, 29 May 2009 15:20:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/29/278958.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/278958.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/29/278958.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/278958.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/278958.htmlC璇█涓璁瞫tatic鍙橀噺鍜宻tatic鍑芥暟鏈変粈涔堜綔鐢?/strong>
static鍏抽敭瀛楁湁涓ょ鎰忔€?浣犵湅涓婁笅鏂囨潵鍒ゆ柇

1,琛ㄧず鍙橀噺鏄潤鎬佸瓨鍌ㄥ彉閲?
琛ㄧず鍙橀噺瀛樻斁鍦ㄩ潤鎬佸瓨鍌ㄥ尯.
2,琛ㄧず璇ュ彉閲忔槸鍐呴儴榪炴帴
(榪欑鎯呭喌鏄寚璇ュ彉閲忎笉鍦ㄤ換浣晎}涔嬪唴,灝辮薄鍏ㄥ眬鍙橀噺閭f牱,榪欐椂鍊欏姞涓妔tatic)
,涔熷氨鏄鍦ㄥ叾瀹冪殑.cpp鏂囦歡涓?璇ュ彉閲忔槸涓嶅彲瑙佺殑(浣犱笉鑳界敤).

褰搒tatic鍔犲湪鍑芥暟鍓嶉潰鐨勬椂鍊?
琛ㄧず璇ュ嚱鏁版槸鍐呴儴榪炴帴,涔嬪湪鏈枃浠朵腑鏈夋晥,鍒殑鏂囦歡涓笉鑳藉簲鐢ㄨ鍑芥暟.
涓嶅姞static鐨勫嚱鏁伴粯璁や負鏄叏灞€鐨?
涔熷氨鏄鍦ㄥ叾浠栫殑.cpp涓彧瑕佺敵鏄庝竴涓嬭繖涓嚱鏁?灝卞彲浠ヤ嬌鐢ㄥ畠.

1銆乻tatic鍏ㄥ眬鍙橀噺涓庢櫘閫氱殑鍏ㄥ眬鍙橀噺鏈変粈涔堝尯鍒紵static灞€閮ㄥ彉閲忓拰鏅€氬眬閮ㄥ彉閲忔湁浠€涔堝尯鍒紵static鍑芥暟涓庢櫘閫氬嚱鏁版湁浠€涔堝尯鍒紵
    絳旓細鍏ㄥ眬鍙橀噺(澶栭儴鍙橀噺)鐨勮鏄庝箣鍓嶅啀鍐犱互static 灝辨瀯鎴愪簡闈欐€佺殑鍏ㄥ眬鍙橀噺銆傚叏灞€鍙橀噺鏈韓灝辨槸闈欐€佸瓨鍌ㄦ柟寮忥紝 闈欐€佸叏灞€鍙橀噺褰撶劧涔熸槸闈欐€佸瓨鍌ㄦ柟寮忋€?榪欎袱鑰呭湪瀛樺偍鏂瑰紡涓婂茍鏃犱笉鍚屻€傝繖涓よ€呯殑鍖哄埆铏藉湪浜庨潪闈欐€佸叏灞€鍙橀噺鐨勪綔鐢ㄥ煙鏄暣涓簮紼嬪簭錛屽綋涓€涓簮紼嬪簭鐢卞涓簮鏂囦歡緇勬垚鏃訛紝闈為潤鎬佺殑鍏ㄥ眬鍙橀噺鍦ㄥ悇涓簮鏂囦歡涓兘鏄湁鏁堢殑銆?鑰岄潤鎬佸叏灞€鍙橀噺鍒欓檺鍒朵簡鍏朵綔鐢ㄥ煙錛?鍗沖彧鍦ㄥ畾涔夎鍙橀噺鐨勬簮鏂囦歡鍐呮湁鏁堬紝 鍦ㄥ悓涓€婧愮▼搴忕殑鍏跺畠婧愭枃浠朵腑涓嶈兘浣跨敤瀹冦€傜敱浜庨潤鎬佸叏灞€鍙橀噺鐨勪綔鐢ㄥ煙灞€闄愪簬涓€涓簮鏂囦歡鍐咃紝鍙兘涓鴻婧愭枃浠跺唴鐨勫嚱鏁板叕鐢紝 鍥犳鍙互閬垮厤鍦ㄥ叾瀹冩簮鏂囦歡涓紩璧烽敊璇€?br />     浠庝互涓婂垎鏋愬彲浠ョ湅鍑猴紝 鎶婂眬閮ㄥ彉閲忔敼鍙樹負闈欐€佸彉閲忓悗鏄敼鍙樹簡瀹冪殑瀛樺偍鏂瑰紡鍗蟲敼鍙樹簡瀹冪殑鐢熷瓨鏈熴€傛妸鍏ㄥ眬鍙橀噺鏀瑰彉涓洪潤鎬佸彉閲忓悗鏄敼鍙樹簡瀹冪殑浣滅敤鍩燂紝 闄愬埗浜嗗畠鐨勪嬌鐢ㄨ寖鍥淬€?br />     static鍑芥暟涓庢櫘閫氬嚱鏁頒綔鐢ㄥ煙涓嶅悓銆俿tatic鍑芥暟浠呭湪鏈枃浠朵腑浣跨敤銆傚彧鍦ㄥ綋鍓嶆簮鏂囦歡涓嬌鐢ㄧ殑鍑芥暟搴旇璇存槑涓哄唴閮ㄥ嚱鏁?static)錛屽唴閮ㄥ嚱鏁板簲璇ュ湪褰撳墠婧愭枃浠朵腑璇存槑鍜屽畾涔夈€傚浜庡彲鍦ㄥ綋鍓嶆簮鏂囦歡浠ュ浣跨敤鐨勫嚱鏁幫紝搴旇鍦ㄤ竴涓ご鏂囦歡涓鏄庯紝瑕佷嬌鐢ㄨ繖浜涘嚱鏁扮殑婧愭枃浠惰鍖呭惈榪欎釜澶存枃浠?br />     static鍏ㄥ眬鍙橀噺涓庢櫘閫氱殑鍏ㄥ眬鍙橀噺鏈変粈涔堝尯鍒細static鍏ㄥ眬鍙橀噺鍙垵浣垮寲涓€嬈★紝闃叉鍦ㄥ叾浠栨枃浠跺崟鍏冧腑琚紩鐢?
    static灞€閮ㄥ彉閲忓拰鏅€氬眬閮ㄥ彉閲忔湁浠€涔堝尯鍒細static灞€閮ㄥ彉閲忓彧琚垵濮嬪寲涓€嬈★紝涓嬩竴嬈′緷鎹笂涓€嬈$粨鏋滃€鹼紱
    static鍑芥暟涓庢櫘閫氬嚱鏁版湁浠€涔堝尯鍒細static鍑芥暟鍦ㄥ唴瀛樹腑鍙湁涓€浠斤紝鏅€氬嚱鏁板湪姣忎釜琚皟鐢ㄤ腑緇存寔涓€浠芥嫹璐?/p>

2銆佸浣曞紩鐢ㄤ竴涓凡緇忓畾涔夎繃鐨勫叏灞€鍙橀噺錛?/strong>
     絳旓細extern
     鍙互鐢ㄥ紩鐢ㄥご鏂囦歡鐨勬柟寮忥紝涔熷彲浠ョ敤extern鍏抽敭瀛楋紝濡傛灉鐢ㄥ紩鐢ㄥご鏂囦歡鏂瑰紡鏉ュ紩鐢ㄦ煇涓湪澶存枃浠朵腑澹版槑鐨勫叏灞€鍙樼悊錛屽亣瀹氫綘灝嗛偅涓彉鍐欓敊浜嗭紝閭d箞鍦ㄧ紪璇戞湡闂?浼氭姤閿欙紝濡傛灉浣犵敤extern鏂瑰紡寮曠敤鏃訛紝鍋囧畾浣犵姱浜嗗悓鏍風殑閿欒錛岄偅涔堝湪緙栬瘧鏈熼棿涓嶄細鎶ラ敊錛岃€屽湪榪炴帴鏈熼棿鎶ラ敊銆?/p>

3銆佸叏灞€鍙橀噺鍙笉鍙互瀹氫箟鍦ㄥ彲琚涓?C鏂囦歡鍖呭惈鐨勫ご鏂囦歡涓紵涓轟粈涔堬紵
    絳旓細鍙互錛屽湪涓嶅悓鐨凜鏂囦歡涓互static褰㈠紡鏉ュ0鏄庡悓鍚嶅叏灞€鍙橀噺銆?br />     鍙互鍦ㄤ笉鍚岀殑C鏂囦歡涓0鏄庡悓鍚嶇殑鍏ㄥ眬鍙橀噺錛屽墠鎻愭槸鍏朵腑鍙兘鏈変竴涓狢鏂囦歡涓姝ゅ彉閲忚祴鍒濆€鹼紝姝ゆ椂榪炴帴涓嶄細鍑洪敊銆?/p>

 



]]>
*杞?C++ 瀵硅薄鐨勫唴瀛樺竷灞€(涓?http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270922.htmlFrank_FangFrank_FangFri, 15 May 2009 12:14:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270922.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/270922.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270922.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270922.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/270922.html闃呰鍏ㄦ枃

]]>
*杞細C++瀵硅薄鐨勫唴瀛樺竷灞€(涓?http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270916.htmlFrank_FangFrank_FangFri, 15 May 2009 11:16:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270916.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/270916.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270916.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270916.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/270916.html闃呰鍏ㄦ枃

]]>
C++鍑芥暟榪斿洖鑷敱鍐呭瓨鍖洪棿鐨勫璞$殑寮曠敤http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270125.htmlFrank_FangFrank_FangMon, 11 May 2009 15:01:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270125.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/270125.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270125.html#Feedback2http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270125.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/270125.htmlEffective C++
鏉℃31: 鍗冧竾涓嶈榪斿洖灞€閮ㄥ璞$殑寮曠敤錛屼篃涓嶈榪斿洖鍑芥暟鍐呴儴鐢╪ew鍒濆鍖栫殑鎸囬拡鐨勫紩鐢?/strong>

 1 #include<iostream>
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 class SimpleCat
 7 {
 8 public:
 9     SimpleCat(void);
10     SimpleCat(int age,int weight);
11     int GetAge(){return itsAge;}
12     int GetWeight(){return itsWeight;}
13 
14 private:
15     int itsAge;
16     int itsWeight;
17     //int itsAge=5;//1>.\SimpleCat.cpp(14) : error C2864: “SimpleCat::itsAge”: 鍙湁闈欐€佸父閲忔暣鍨嬫暟鎹垚鍛樻墠鍙互鍦ㄧ被涓垵濮嬪寲
18 
19 public:
20     virtual ~SimpleCat(void);
21 };
22 
23 SimpleCat::SimpleCat(void)
24 {
25     cout<<"SimpleCat constructor "<<this<<endl;
26 }
27 
28 SimpleCat::SimpleCat(int age,int weight)
29 {
30     cout<<"SimpleCat constructor "<<this<<endl;
31     itsAge = age;
32     itsWeight = weight;
33 }
34 
35 SimpleCat::~SimpleCat(void)
36 {
37     cout<<"SimpleCat destructor"<<this<<endl;
38 }
39 
40 SimpleCat &TheFunction();
41 
42 int main()
43 {
44     SimpleCat myCat;
45     cout<<myCat.GetAge()<<endl;//榪欎釜鍊煎尯鍒簬java涓嶆槸璧嬪垵鍊間負0鐨?鑰屾槸涓€涓殢鏈虹殑鍊?br /> 46 //    cout<<myCat.itsAge<<endl;
47 
48     cout<<"------------------------"<<endl;
49     SimpleCat &rCat = TheFunction();
50     int age = rCat.GetAge();
51     cout<<"rCat is "<<age<<"yeas old!"<<endl;
52     cout<<"&rCat:  "<<&rCat<<endl;
53     SimpleCat *pCat = &rCat;
54     //delete rCat;//涓嶈兘瀵瑰紩鐢ㄤ嬌鐢╠elete
55     delete pCat;
56     //delete濂藉儚娌℃湁閲婃斁鍐呭瓨錛屾€庝箞鑾峰彇鐨勮繕鏄師鏉ョ殑鍊?br /> 57     //鍙兘鍦ㄨ繖涓唴瀛樺尯鍩熷瓨鏀劇殑榪樻槸鍘熸潵鐨?鍏坣ew string鍚庡啀璋冪敤涔熸病鍙橈紝涓庣紪璇戝櫒鏈夊叧榪樻槸浠€涔??
58     for(int i =0;i<10;i++)
59     {
60         //鎯抽€氳繃鍒涘緩string瀵硅薄鏉ュ~鍏呬箣鍓嶇殑鍐呭瓨鍖洪棿錛屽ソ鍍忔病鐢?/span>
61         string *= new string("abcdefghijklmn");
62     }
63 
64     //榪欐椂闂鏉ヤ簡,rCat.getAge()涓?23浜?/span>
65     SimpleCat *pSecond = new SimpleCat(123,444);
66     
67     cout<<"delete pCat鍚庡啀浣跨敤rCat寮曠敤浼氬彂鐢熶粈涔堥棶棰???"<<endl;
68     cout<<"delete pCat鍚?nbsp;&rCat"<<&rCat<<endl;
69     cout<<"delete pCat鍚?nbsp;rCat.age"<<rCat.GetAge()<<endl;
70 
71     cout<<"--------------------------"<<endl;
72     SimpleCat myCat2 = TheFunction();//榪欎釜浼氬彂鐢熷唴瀛樻硠婕忥紝鍦ㄥ嚱鏁頒腑鐢寵鐨勫唴瀛樹細寰椾笉鍒伴噴鏀?br /> 73                                      //myCat2鏄€氳繃榛樿鐨勬嫹璐濆嚱鏁版潵榪涜鍒濆鍖栫殑
74     cout<<"myCat2鐨勫湴鍧€鏄?nbsp;"<<&myCat2<<endl;
75     return 0;
76 }
77 
78 
79 //榪欎釜鍑芥暟鍦ㄨ繑鍥炴椂錛屾槸鍚︿細鍒涘緩涓€涓復鏃跺紩鐢ㄥ彉閲???
80 //TheFunctionTwo(SimpleCat &simpleCat)榪欎釜鍑芥暟鍦ㄤ紶閫掑弬鏁版椂鏄惁浼氬垱寤轟竴涓復鏃剁殑寮曠敤鍙橀噺
81 //涓存椂鐨勫紩鐢ㄥ彉閲忕殑浣滅敤鍩熻寖鍥存槸浠€涔?/span>
82 SimpleCat &TheFunction()
83 {
84     SimpleCat *pFrisky = new SimpleCat(5,9);
85     cout<<"pFrisky: "<<pFrisky<<endl;
86     return *pFrisky;
87 }
88 
89 
90 

鍑芥暟TheFunction()鐨勮繑鍥炲€艱璧嬪€肩粰瀵筍impleCat綾葷殑涓€涓紩鐢?鐒跺悗浣跨敤榪欎釜寮曠敤鏉ヨ幏寰楀璞$殑鎴愬憳鍙橀噺age鐨勫€?br /> 涓轟簡璇佹槑鍑芥暟TheFunction()鍦ㄨ嚜鐢卞瓨鍌ㄥ尯涓垱寤虹殑瀵硅薄琚祴緇欎簡鍑芥暟main()澹版槑鐨勫紩鐢紝瀵箁Cat閲囧彇鍙栧潃榪愮畻錛屽緢鏄庢樉錛屽畠鏄劇ず浜嗗叾寮曠敤鐨勫璞$殑鍦板潃錛屽茍涓斿拰鑷敱瀛樺偍鍖轟腑鐨勫璞$殑鍦板潃鐩稿尮閰嶃€?br /> 榪欎竴鍒囩湅涓婂幓閮藉緢姝g‘錛屼絾鏄浣曢噴鏀捐鍗犵敤鐨勫唴瀛橈紝瀵逛簬寮曠敤鏄笉鑳戒嬌鐢╠elete榪愮畻絎︾殑錛屼竴涓瑪璁拌仾鏄庣殑鏂規硶鏄0鏄庡彟涓€涓寚閽堬紝騫朵嬌鐢ㄧ敱rCat寰楀埌鐨勫湴鍧€瀵瑰畠榪涜鍒濆鍖栥€傝繖鏍峰仛紜疄鍙互閲婃斁鍐呭瓨錛岄伩鍏嶄簡鍐呭瓨鐨勬硠婕忥紝浣嗘槸瀛樺湪涓€涓皬闂錛屽湪delete pCat鍚庯紝rCat寮曠敤鍙堜唬琛ㄤ粈涔堝憿錛燂紵寮曠敤蹇呴』鏄竴涓疄闄呭璞$殑鍒悕錛涘鏋滃紩鐢ㄤ竴涓┖瀵硅薄鐨勮瘽錛堢幇鍦ㄥ氨鏄繖縐嶆儏鍐碉級閭d箞紼嬪簭灝辨槸鏃犳晥鐨勩€傘€?/p>

娉ㄦ剰錛氫笉鑳借繃鍒嗗幓寮鴻皟涓€涓嬌鐢ㄥ絀哄璞$殑寮曠敤鐨勭▼搴忥紝涔熻鑳介€氳繃緙栬瘧錛屼絾鏄嵆浣跨▼搴忛€氳繃浜嗙紪璇戯紝瀹冧粛鐒舵槸鐥呮€佺殑錛岃€屼笖鎵ц緇撴灉涔熸槸涓嶅彲棰勬枡鐨?/p>

鍏充簬榪欎釜闂錛屽瓨鍦ㄨ繖3縐嶈В鍐蟲柟妗?br /> 絎?縐嶆槸鍦ㄧ49琛屽0鏄庝竴涓猄impleCat綾誨璞★紝鐒跺悗浠庡嚱鏁癟heFunction()涓噰鍙栧€間紶閫掔殑鏂瑰紡榪斿洖瀹冪殑灞€閮ㄥ璞★紝榪欐椂涓嶈兘鍦ㄨ嚜鐢卞瓨鍌ㄥ尯鏉ュ垎閰嶅璞$┖闂達紝涓嶈兘浣跨敤new錛?br /> 絎?縐嶆槸鍦ㄥ嚱鏁癟heFunction()涓0鏄庝竴涓綅浜庤嚜鐢卞瓨鍌ㄥ尯涓殑SimpleCat綾葷殑瀵硅薄錛屼絾鏄鍑芥暟榪斿洖涓€涓寚鍚戝唴瀛樺尯鍩熺殑鎸囬拡錛岀劧鍚庡畬鎴愬瀵硅薄鐨勬搷浣滐紝璋冪敤鍑芥暟鍙互鍒犻櫎榪欎釜鎸囬拡
絎?縐嶅彲琛岀殑鏂規錛屼篃鏄紜殑錛屾槸鍦ㄨ皟鐢ㄥ嚱鏁頒腑澹版槑瀵硅薄錛岀劧鍚庨€氳繃寮曠敤鐨勬柟寮忔妸瀹冧紶閫掔粰鍑芥暟TheFunction()


涔熸剰鍛沖湪main鍑芥暟涓笉鑳借繖鏍蜂嬌鐢ㄤ簡



]]>
java瀵硅薄鐨勫疄渚嬪寲榪囩▼,鎴愬憳鍙橀噺鐨勫垵濮嬪寲欏哄簭http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270110.htmlFrank_FangFrank_FangMon, 11 May 2009 13:16:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270110.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/270110.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270110.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270110.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/270110.htmlJAVA涓瓙綾誨璞$殑瀹炰緥鍖栬繃紼?/span>

 Person.java

class Person
{
 
public String name = "not know";//4
 public int age = -1 ;//5
 public Person()
 
{    
 }
 
public Person(String name , int age)//3
 {
  
this.name = name ;//6
  this.age = age;//7
 }
 
public void showInfo()
 
{
  System.out.println("Name is :"+name+" Age is :"+age);
 }
}
class Student extends Person
{
 
public String school = "not konwn";//8
    public Student()
    
{
     
super();    
    }
    
public Student (String name ,int age, String school)  //1
    {
     
super(name,age);  //2
     this.school = school;//9
    }
    
public void showInfo()
    
{
     
super.showInfo();
     System.out.println("School is :"+school);
    }
}

AllTest.java

class AllTest
{
 
public static void main(String[]args)
 
{
  Student stA = 
new Student("wanghao",20,"qinghuadaxue"); //0
 }
}

鍏惰鍙ョ殑鎵ц欏哄簭鏄€庢牱鐨勫憿錛?/span>
java
涓紝鍦ㄤ嬌鐢?/span>new鎿嶄綔絎﹀垱寤轟竴涓被鐨勫疄渚嬪璞$殑鏃跺€欙紝寮€濮嬪垎閰嶇┖闂村茍灝嗘垚鍛樺彉閲忓垵濮嬪寲涓?/span>榛樿鐨?/span>鏁板€?/span>錛屾敞鎰忚繖閲屽茍涓嶆槸鎸囧皢鍙橀噺鍒濆鍖栦負鍦ㄥ彉閲忓畾涔夊鐨勫垵濮嬪€鹼紝鑰?strong>鏄粰鏁村艦璧嬪€?/span>
0錛岀粰瀛楃涓茶祴鍊?/span>null 榪欎竴鐐逛簬C++涓嶅悓錛岋紙student.name = null , student.age = 0 錛?/span>
鐒跺悗鍦ㄨ繘鍏ョ被鐨勬瀯閫犲嚱鏁般€?/span>

鍦ㄦ瀯閫犲嚱鏁伴噷闈紝棣栧厛瑕佹鏌ユ槸鍚︽湁this鎴栬€?/span>super璋冪敤錛?/span>this璋冪敤鏄畬鎴愭湰綾繪湰韜殑鏋勯€犲嚱鏁頒箣闂寸殑璋冪敤錛?/span>super璋冪敤鏄畬鎴愬鐖剁被鐨勮皟鐢ㄣ€?strong>浜岃€呭彧鑳藉嚭鐜頒竴涓紝騫朵笖鍙兘浣滀負鏋勯€犲嚱鏁扮殑絎竴鍙ュ嚭鐜般€?/span>鍦ㄨ皟鐢?/span>this鍜?/span>super鐨勬椂鍊欏疄鐜扮▼搴忕殑璺寵漿錛岃漿鑰屾墽琛岃璋冪敤鐨?/span>this鏋勯€犲嚱鏁版垨鑰?/span>super鏋勯€犲嚱鏁般€?/span>
鍦?/span>this鍜?/span>super鎵ц瀹屾瘯錛?strong>紼嬪簭杞€屾墽琛屽湪綾誨畾涔夌殑鏃跺€欒繘琛岀殑鍙橀噺鍒濆鍖栧伐浣?/span>銆?/span>
榪欎釜鎵ц瀹屾瘯錛屾墠鏄瀯閫犲嚱鏁頒腑鍓╀笅鐨勪唬鐮佺殑鎵ц銆?/span>
鎵ц欏哄簭宸茬粡鐢ㄧ豢鑹叉爣鍑恒€?/span>

Order of initialization
Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called鈥攅ven the constructor. For example: Feedback
//: c04:OrderOfInitialization.java
// Demonstrates initialization order.
import com.bruceeckel.simpletest.*;

// When the constructor is called to create a
// Tag object, you'll see a message:
class Tag {
  Tag(int marker) {
    System.out.println("Tag(" + marker + ")");
  }
}

class Card {
  Tag t1 = new Tag(1); // Before constructor
  Card() {
    // Indicate we're in the constructor:
    System.out.println("Card()");
    t3 = new Tag(33); // Reinitialize t3
  }
  Tag t2 = new Tag(2); // After constructor
  void f() {
    System.out.println("f()");
  }
  Tag t3 = new Tag(3); // At end
}

public class OrderOfInitialization {
  static Test monitor = new Test();
  public static void main(String[] args) {
    Card t = new Card();
    t.f(); // Shows that construction is done
    monitor.expect(new String[] {
      "Tag(1)",
      "Tag(2)",
      "Tag(3)",
      "Card()",
      "Tag(33)",
      "f()"
    });
  }
} ///:~


//: c04:StaticInitialization.java
// Specifying initial values in a class definition.
class Bowl {
  Bowl(int marker) {
    System.out.println("Bowl(" + marker + ")");
  }
  void f(int marker) {
    System.out.println("f(" + marker + ")");
  }
}

class Table {
  static Bowl b1 = new Bowl(1);
  Table() {
    System.out.println("Table()");
    b2.f(1);
  }
  void f2(int marker) {
    System.out.println("f2(" + marker + ")");
  }
  static Bowl b2 = new Bowl(2);
}

class Cupboard {
  Bowl b3 = new Bowl(3);
  static Bowl b4 = new Bowl(4);
  Cupboard() {
    System.out.println("Cupboard()");
    b4.f(2);
  }
  void f3(int marker) {
    System.out.println("f3(" + marker + ")");
  }
  static Bowl b5 = new Bowl(5);
}

public class StaticInitialization {
  static Test monitor = new Test();
  public static void main(String[] args) {
    System.out.println("Creating new Cupboard() in main");
    new Cupboard();
    System.out.println("Creating new Cupboard() in main");
    new Cupboard();
    t2.f2(1);
    t3.f3(1);
    monitor.expect(new String[] {
      "Bowl(1)",
      "Bowl(2)",
      "Table()",
      "f(1)",
      "Bowl(4)",
      "Bowl(5)",
      "Bowl(3)",
      "Cupboard()",
      "f(2)",
      "Creating new Cupboard() in main",
      "Bowl(3)",
      "Cupboard()",
      "f(2)",
      "Creating new Cupboard() in main",
      "Bowl(3)",
      "Cupboard()",
      "f(2)",
      "f2(1)",
      "f3(1)"
    });
  }
  static Table t2 = new Table();
  static Cupboard t3 = new Cupboard();
} ///:~


緇ф壙鎯呭喌涓嬬殑鍒濆鍖?br /> 浜嗚В涓€涓嬪寘鎷戶鎵垮湪鍐呯殑鍒濆鍖栫殑榪囩▼灝嗘槸闈炲父鏈夌泭鐨勶紝榪欐牱灝辮兘鏈変釜鎬?br /> 浣撶殑浜嗚В銆傜湅鐪嬩笅闈㈣繖孌典唬鐮侊細
//: c06:Beetle.java
// The full process of initialization.
class Insect {
  protected static Test monitor = new Test();
  private int i = 9;
  protected int j;
  Insect() {
    System.out.println("i = " + i + ", j = " + j);
    j = 39;
  }
  private static int x1 = print("static Insect.x1 initialized");
  static int print(String s) {
    System.out.println(s);
    return 47;
  }
}

public class Beetle extends Insect {
  private int k = print("Beetle.k initialized");
  public Beetle() {
    System.out.println("k = " + k);
    System.out.println("j = " + j);
  }
  private static int x2 = print("static Beetle.x2 initialized");
  public static void main(String[] args) {
    System.out.println("Beetle constructor");
    Beetle b = new Beetle();
    monitor.expect(new String[] {
      "static Insect.x1 initialized",
      "static Beetle.x2 initialized",
      "Beetle constructor",
      "i = 9, j = 0",
      "Beetle.k initialized",
      "k = 47",
      "j = 39"
    });
  }
} ///:~

褰撲綘鐢?/span>Java 榪愯Beetle 鐨勬椂鍊欙紝絎竴浠朵簨灝辨槸璁塊棶浜?/span>Beetel.main( )(榪欐槸涓€涓?/span>static 鏂?span style="color: blue">娉?/span>)錛屼簬鏄杞藉櫒(loader)灝變細涓?/span>

浣犲鎵劇粡緙栬瘧鐨?/span>Beetle 綾葷殑浠g爜(涔熷氨鏄?/span>Beetle.class 鏂囦歡)銆傚湪瑁呰澆鐨勮繃紼嬩腑錛岃杞藉櫒娉ㄦ剰鍒板畠鏈変竴涓熀綾?/span>(涔熷氨鏄?/span>extends 鎵€瑕佽〃紺虹殑鎰忔€?/span>)錛屼簬鏄畠鍐嶈杞藉熀綾匯€備笉綆′綘鍒涗笉鍒涘緩鍩虹被瀵硅薄錛岃繖涓繃紼嬫€諱細鍙戠敓銆?/span>(璇曡瘯鐪嬶紝鎶婂垱寤哄璞$殑閭e彞娉ㄩ噴鎺夛紝鐪嬬湅浼氭湁浠€涔堢粨鏋溿€?/span>)濡傛灉鍩虹被榪樻湁鍩虹被錛岄偅涔堣繖絎簩涓熀綾諱篃浼氳瑁呰澆錛屼互姝ょ被鎺?span style="color: blue">銆?strong>涓嬩竴姝ワ紝瀹冧細鎵ц“鏍瑰熀綾?/strong>(root bas e class)(榪欓噷灝辨槸Insect)鐨?/span>static 鍒濆鍖栵紝鐒跺悗鏄笅涓€涓淳鐢熺被鐨?/span>static 鍒濆鍖栵紝浠ユ綾繪帹銆傝繖涓『搴忛潪甯擱噸瑕侊紝鍥犱負媧劇敓綾葷殑“闈欐€佸垵濮嬪寲(鍗沖墠闈㈣鐨?/span>static 鍒濆鍖?/span>)”鏈夊彲鑳借渚濊禆鍩虹被鎴愬憳鐨勬紜垵濮嬪寲銆?/span>

鐜板湪鎵€鏈夊繀瑕佺殑綾婚兘宸茬粡瑁呰澆緇撴潫錛屽彲浠ュ垱寤哄璞′簡銆?/span>棣栧厛錛屽璞¢噷鐨勬墍鏈夌殑primitive 閮戒細琚鎴愬畠浠殑緙虹渷鍊鹼紝鑰?/span>reference 涔熶細琚鎴?/span>null鈥斺€旇繖涓繃紼嬫槸涓€鐬棿瀹屾垚鐨勶紝瀵硅薄鐨勫唴瀛樹細琚粺涓€鍦拌緗垚“涓よ繘鍒剁殑闆?/span>(binary zero)”銆?strong>濡傛灉鏈夊鎴愬憳鍙橀噺璧嬪垵鍊鹼紝鍒欏鎴愬憳鍙橀噺榪涜璧嬪€鹼紝鐒跺悗璋冪敤鍩虹被鐨勬瀯閫犲嚱鏁般€傝皟鐢ㄦ槸鑷姩鍙戠敓鐨勶紝浣嗘槸浣犲彲浠ヤ嬌鐢?/span>super 鏉ユ寚瀹氳皟鐢ㄥ摢涓瀯閫犲嚱鏁?/span>(涔熷氨Beetle( )鏋勯€犲嚱鏁版墍鍋氱殑絎竴浠朵簨)銆傚熀綾葷殑鏋勯€犺繃紼嬩互鍙婃瀯閫犻『搴忥紝鍚屾淳鐢熺被鐨勭浉鍚屻€傚熀綾繪瀯閫犲嚱鏁拌繍琛屽畬姣曚箣鍚庯紝浼氭寜鐓у悇涓彉閲忕殑瀛楅潰欏哄簭榪涜鍒濆鍖栥€傛渶鍚庝細鎵ц鏋勯€犲嚱鏁扮殑鍏朵綑閮ㄥ垎銆?/span>



]]>*C++涓嚱鏁板€艱繑鍥炵殑榪囩▼涓殑闂錛屾槸鍚﹀垱寤轟復鏃跺彉閲?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270051.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Mon, 11 May 2009 07:22:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270051.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/270051.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270051.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270051.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/270051.html</trackback:ping><description><![CDATA[<p>Sales_item operator+(const Sales_item& lhs,const Sales_item& rhs) <br /> { <br />     Sales_item ret(lhs) ; <br />     ret+= rhs ; <br />     return ret ; <br /> } <br /> 鎬庝箞鍙互榪斿洖涓€涓眬閮ㄥ彉閲忓憿錛燂紵錛燂紵錛燂紵 <br /> 渚嬪 <br /> Sales_item item1,item2,item3 ; <br /> ......(item2,item3鍒濆鍖栨垚鍛樺彉閲? <br /> item1 = item2 + item3 ; </p> <p><br /> 娌℃湁闂錛屽畠榪斿洖鐨勬槸涓€涓€鹼紝鑰屼笉鏄紩鐢紝鎵€浠ユ槸姝g‘鐨勩€傚湪item1 = item2 + item3 涓彂鐢熶簡涓€嬈″€兼嫹璐?璧嬪€?錛屼篃灝辨槸灝唅tem2+item3榪斿洖鐨勫眬閮ㄥ彉閲忔嫹璐濈粰浜唅tem1涔嬪悗錛屽眬閮ㄥ彉閲忕殑浣滅敤鍩熺粨鏉?nbsp;<br />  <br /> <span style="color: red"><strong>鍙互榪斿洖灞€閮ㄥ彉閲忥紝浣嗘槸涓嶈兘榪斿洖灞€閮ㄥ彉閲忕殑寮曠敤銆傜悊瑙e尯鍒嗗€煎拰寮曠敤榪欎袱涓蹇墊槸瀛︿範C++鐨勪竴澶у叧閿紝鏄庣櫧榪欎袱涓蹇典箣鍚庯紝浣犲氨浼氱悊瑙d負浠€涔圕++鐨勭被閲岄潰闇€瑕佹湁鎷瘋礉鏋勯€犲嚱鏁幫紝璧嬪€兼搷浣滅錛屾瀽鏋勫嚱鏁頒笁涓厓绱犱簡浠ュ強鍏跺畠鐨勪竴浜涚█閲屽彜鎬殑鐢ㄦ硶鍜屽繝鍛婁簡 <br /> </strong></span>-- </p> <p> </p> <p>Sales_item operator+(const Sales_item& lhs,const Sales_item& rhs)  <br /> {  <br />     Sales_item ret(lhs) ;  <br />     ret+= rhs ;  <br />     return ret ;  <br /> } <br /> 鍦╮eturn ret 鏃跺茍涓嶆槸綆€鍗曠殑榪斿洖榪欎釜灞€閮ㄥ彉閲忥紝鑰屾槸榪斿洖鐨勬槸ret鐨勪竴涓壇鏈瑃emp, <br /> temp鏄€氳繃鎷瘋礉鏋勯€犲嚱鏁板疄鐜扮殑 鍗?Sales_item temp(ret);涔熷氨鏄鍦ㄤ富鍑芥暟涓敤鐨勫叾瀹炴槸涓猼emp,鑰宺et鏃╁湪operator+璋冪敤瀹屾瘯鍚庡氨閲婃斁浜?span style="color: red">錛岃€宼emp榪欎釜 瀵硅薄涓€鐩村湪涓誨嚱鏁頒腑璐瓨錛堣櫧鐒舵樉紺烘槸鐪嬩笉鍒扮殑 錛?strong><span style="color: red"><strong> </strong><strong> ????</strong></span></strong></span></p> <p><br /> 鍡棷錛屼復鏃跺璞′竴鐩存槸涓湁浜夎鐨勮瘽棰樸€?span style="color: red"><strong>temp鍙槸涓存椂鏋勯€犵殑錛屽湪璧嬪€煎畬姣曚箣鍚庡畠灝辨瀽鏋勪簡錛屼笉鏄竴鐩村湪涓誨嚱鏁頒腑璐瓨鐨勩€傝繖涓復鏃跺璞$殑浣滅敤鍩熸槸浠€涔堬紵錛燂紵<br /> </strong></span>鍦ㄤ綘鎻愮殑榪欑鎯呭喌涓嬶紝鍏跺疄緙栬瘧鍣ㄦ槸鍙互浼樺寲鎺夎繖涓壇鏈殑錛屼絾鏄彲鎯淐++鏍囧噯鍙厑璁鎬紭鍖?nbsp; “Sales_item item1=item2+item3” 榪欑鎯呭喌鐨?涔熷氨鏄湪鎷瘋礉鏋勯€犺繖縐嶆儏鍐典笅錛岀紪璇戝櫒閮戒笉鍐嶄駭鐢熷壇鏈紝鑰岃祴鍊艱繕鏄笉琛岀殑銆備篃璁稿湪鏈潵鐨凜++鏍囧噯鐢卞彲鑳介€氳繃鎷撳睍璇箟鏉ユ秷闄や復鏃跺璞★紝姣曠珶涓存椂瀵硅薄鎴愪負褰卞搷C++鏁堢巼鐨勪竴涓富瑕佸洜绱犮€?<br /> -- </p> <p>???<br /> <span style="color: red"><strong><span style="color: #0000ff">鐩墠鐨凜++鏍囧噯涓嶅厑璁稿湪璧嬪€肩殑鏃跺€欎紭鍖栨帀鍑芥暟榪斿洖鐨勫壇鏈紝涔熷氨鏄笅闈㈣繖涓ょ鎯呭喌鏄笉涓€鏍風殑 <br />  <br /> //榪欐槸璧嬪€肩粰item1錛岀洰鍓嶇殑鏍囧噯涓嬫槸浼氫駭鐢熷壇鏈?temp<br /> Sales_item item1,item2,item3; <br /> item1=item2+item3; <br />  <br /> //榪欐槸鎷瘋礉鏋勯€爄tem1錛岀紪璇戝櫒閫氬父浼氫紭鍖?span style="color: #ff0000">(榪欎釜浼樺寲鍖哄埆涓嬮潰鎵€璇寸殑NRV浼樺寲)</span>鎺夊壇鏈紝涔熷氨鏄笉浜х敓鍓湰 temp<br /> Sales_item item2,item3; <br /> Sales_item item1=item2+item3; <br /> </span><br /> 鐪嬩竴涓嬩唬鐮佷腑鐨凾heFunctionTwo();<br /> </strong></p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><strong><span style="color: #008080">  1</span> <span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #000000">iostream</span><span style="color: #000000">></span></strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">  2</span> <span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">></span></strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">  3</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">  4</span> <span style="color: #0000ff">using</span><span style="color: #000000"> </span><span style="color: #0000ff">namespace</span></strong><strong><span style="color: #000000"> std;<br /> </span><span style="color: #008080">  5</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">  6</span> <span style="color: #0000ff">class</span></strong><strong><span style="color: #000000"> SimpleCat<br /> </span><span style="color: #008080">  7</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080">  8</span> <span style="color: #0000ff">public</span></strong><strong><span style="color: #000000">:<br /> </span><span style="color: #008080">  9</span> <span style="color: #000000">    SimpleCat(</span><span style="color: #0000ff">void</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080"> 10</span> <span style="color: #000000">    SimpleCat(</span><span style="color: #0000ff">int</span><span style="color: #000000"> age,</span><span style="color: #0000ff">int</span></strong><strong><span style="color: #000000"> weight);<br /> </span><span style="color: #008080"> 11</span> <span style="color: #000000">    SimpleCat(SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">rsc);<br /> </span><span style="color: #008080"> 12</span> <span style="color: #000000">    SimpleCat </span><span style="color: #000000">&</span><span style="color: #000000"> </span><span style="color: #0000ff">operator</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #0000ff">const</span><span style="color: #000000"> SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">rhs);<br /> </span><span style="color: #008080"> 13</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> GetAge(){</span><span style="color: #0000ff">return</span></strong><strong><span style="color: #000000"> itsAge;}<br /> </span><span style="color: #008080"> 14</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> GetWeight(){</span><span style="color: #0000ff">return</span></strong><strong><span style="color: #000000"> itsWeight;}<br /> </span><span style="color: #008080"> 15</span> <span style="color: #000000">    </span><span style="color: #0000ff">void</span><span style="color: #000000"> SetAge(</span><span style="color: #0000ff">int</span><span style="color: #000000"> age){itsAge </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> age;}<br /> </span><span style="color: #008080"> 16</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 17</span> <span style="color: #0000ff">private</span></strong><strong><span style="color: #000000">:<br /> </span><span style="color: #008080"> 18</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span></strong><strong><span style="color: #000000"> itsAge;<br /> </span><span style="color: #008080"> 19</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span></strong><strong><span style="color: #000000"> itsWeight;<br /> </span><span style="color: #008080"> 20</span> <span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">int itsAge=5;</span><span style="color: #008000">//</span><span style="color: #008000">1>.\SimpleCat.cpp(14) : error C2864: “SimpleCat::itsAge”: 鍙湁闈欐€佸父閲忔暣鍨嬫暟鎹垚鍛樻墠鍙互鍦ㄧ被涓垵濮嬪寲</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 21</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 22</span> <span style="color: #0000ff">public</span></strong><strong><span style="color: #000000">:<br /> </span><span style="color: #008080"> 23</span> <span style="color: #000000">    </span><span style="color: #0000ff">virtual</span><span style="color: #000000"> </span><span style="color: #000000">~</span><span style="color: #000000">SimpleCat(</span><span style="color: #0000ff">void</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080"> 24</span> </strong><strong><span style="color: #000000">};<br /> </span><span style="color: #008080"> 25</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 26</span> <span style="color: #000000">SimpleCat::SimpleCat(</span><span style="color: #0000ff">void</span></strong><strong><span style="color: #000000">)<br /> </span><span style="color: #008080"> 27</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080"> 28</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">SimpleCat constructor </span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #0000ff">this</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 29</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080"> 30</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 31</span> <span style="color: #000000">SimpleCat::SimpleCat(</span><span style="color: #0000ff">int</span><span style="color: #000000"> age,</span><span style="color: #0000ff">int</span></strong><strong><span style="color: #000000"> weight)<br /> </span><span style="color: #008080"> 32</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080"> 33</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">SimpleCat constructor </span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #0000ff">this</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 34</span> <span style="color: #000000">    itsAge </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> age;<br /> </span><span style="color: #008080"> 35</span> <span style="color: #000000">    itsWeight </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> weight;<br /> </span><span style="color: #008080"> 36</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080"> 37</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 38</span> <span style="color: #000000">SimpleCat::SimpleCat(SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">rhs)<br /> </span><span style="color: #008080"> 39</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080"> 40</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">SimpleCat copy constructor</span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #0000ff">this</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000"> 浠?nbsp;</span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">rhs</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">鎷瘋礉</span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 41</span> <span style="color: #000000">    itsAge </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> rhs.itsAge;<br /> </span><span style="color: #008080"> 42</span> <span style="color: #000000">    itsWeight </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> rhs.itsWeight;<br /> </span><span style="color: #008080"> 43</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080"> 44</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 45</span> <span style="color: #000000">SimpleCat </span><span style="color: #000000">&</span><span style="color: #000000">SimpleCat::</span><span style="color: #0000ff">operator</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #0000ff">const</span><span style="color: #000000"> SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">rhs)<br /> </span><span style="color: #008080"> 46</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080"> 47</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">SimpleCat閲嶈澆璧嬪€艱繍綆楃</span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #0000ff">this</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000"> 浠?nbsp;</span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">rhs</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">璧嬪€?/span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 48</span> <span style="color: #000000">    itsAge </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> rhs.itsAge;<br /> </span><span style="color: #008080"> 49</span> <span style="color: #000000">    itsWeight </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> rhs.itsWeight;<br /> </span><span style="color: #008080"> 50</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #0000ff">this</span></strong><strong><span style="color: #000000">;<br /> </span><span style="color: #008080"> 51</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080"> 52</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 53</span> <span style="color: #000000">SimpleCat::</span><span style="color: #000000">~</span><span style="color: #000000">SimpleCat(</span><span style="color: #0000ff">void</span></strong><strong><span style="color: #000000">)<br /> </span><span style="color: #008080"> 54</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080"> 55</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">SimpleCat destructor</span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #0000ff">this</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 56</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080"> 57</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 58</span> <span style="color: #000000">SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">TheFunction();<br /> </span><span style="color: #008080"> 59</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 60</span> </strong><strong><span style="color: #000000">SimpleCat TheFunctionTwo();<br /> </span><span style="color: #008080"> 61</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 62</span> <span style="color: #000000">SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">TheFunctionThree();<br /> </span><span style="color: #008080"> 63</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 64</span> <span style="color: #0000ff">void</span></strong><strong><span style="color: #000000"> TheFunctionFour(SimpleCat simpleCat);<br /> </span><span style="color: #008080"> 65</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 66</span> <span style="color: #0000ff">int</span></strong><strong><span style="color: #000000"> main()<br /> </span><span style="color: #008080"> 67</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080"> 68</span> </strong><strong><span style="color: #000000">    SimpleCat myCat;<br /> </span><span style="color: #008080"> 69</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">myCat.GetAge()</span><span style="color: #000000"><<</span><span style="color: #000000">endl;</span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">榪欎釜鍊煎尯鍒簬java涓嶆槸璧嬪垵鍊間負0鐨?鑰屾槸涓€涓殢鏈虹殑鍊?br /> </span><span style="color: #008080"> 70</span> <span style="color: #008000">//</span><span style="color: #008000">    cout<<myCat.itsAge<<endl;</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 71</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 72</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">------------------------</span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 73</span> <span style="color: #000000">    SimpleCat </span><span style="color: #000000">&</span><span style="color: #000000">rCat </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> TheFunction();<br /> </span><span style="color: #008080"> 74</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> age </span><span style="color: #000000">=</span></strong><strong><span style="color: #000000"> rCat.GetAge();<br /> </span><span style="color: #008080"> 75</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">rCat is </span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #000000">age</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">yeas old!</span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 76</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">&rCat:  </span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">rCat</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 77</span> <span style="color: #000000">    SimpleCat </span><span style="color: #000000">*</span><span style="color: #000000">pCat </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">rCat;<br /> </span><span style="color: #008080"> 78</span> <span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">delete rCat;</span><span style="color: #008000">//</span><span style="color: #008000">涓嶈兘瀵瑰紩鐢ㄤ嬌鐢╠elete</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 79</span> </strong><strong><span style="color: #000000">    delete pCat;<br /> </span><span style="color: #008080"> 80</span> <span style="color: #000000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">delete濂藉儚娌℃湁閲婃斁鍐呭瓨錛屾€庝箞鑾峰彇鐨勮繕鏄師鏉ョ殑鍊?br /> </span><span style="color: #008080"> 81</span> <span style="color: #008000">    </span><span style="color: #008000">//</span><span style="color: #008000">鍙兘鍦ㄨ繖涓唴瀛樺尯鍩熷瓨鏀劇殑榪樻槸鍘熸潵鐨?鍏坣ew string鍚庡啀璋冪敤涔熸病鍙橈紝涓庣紪璇戝櫒鏈夊叧榪樻槸浠€涔??</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 82</span> <span style="color: #000000">    </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000"><</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span></strong><strong><span style="color: #000000">)<br /> </span><span style="color: #008080"> 83</span> </strong><strong><span style="color: #000000">    {<br /> </span><span style="color: #008080"> 84</span> <span style="color: #000000">        </span><span style="color: #008000">//</span><span style="color: #008000">鎯抽€氳繃鍒涘緩string瀵硅薄鏉ュ~鍏呬箣鍓嶇殑鍐呭瓨鍖洪棿錛屽ソ鍍忔病鐢?/span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 85</span> <span style="color: #000000">        </span><span style="color: #0000ff">string</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">s </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">abcdefghijklmn</span><span style="color: #000000">"</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080"> 86</span> </strong><strong><span style="color: #000000">    }<br /> </span><span style="color: #008080"> 87</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 88</span> <span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">榪欐椂闂鏉ヤ簡,rCat.getAge()涓?23浜?/span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 89</span> <span style="color: #000000">    SimpleCat </span><span style="color: #000000">*</span><span style="color: #000000">pSecond </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SimpleCat(</span><span style="color: #000000">123</span><span style="color: #000000">,</span><span style="color: #000000">444</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080"> 90</span> </strong><strong><span style="color: #000000">    <br /> </span><span style="color: #008080"> 91</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">delete pCat鍚庡啀浣跨敤rCat寮曠敤浼氬彂鐢熶粈涔堥棶棰???</span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 92</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">delete pCat鍚?nbsp;&rCat</span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">rCat</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 93</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">delete pCat鍚?nbsp;rCat.age</span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #000000">rCat.GetAge()</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 94</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080"> 95</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">--------------------------</span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 96</span> <span style="color: #000000">    SimpleCat myCat2 </span><span style="color: #000000">=</span><span style="color: #000000"> TheFunction();</span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">榪欎釜浼氬彂鐢熷唴瀛樻硠婕忥紝鍦ㄥ嚱鏁頒腑鐢寵鐨勫唴瀛樹細寰椾笉鍒伴噴鏀?br /> </span><span style="color: #008080"> 97</span> <span style="color: #008000">                                     </span><span style="color: #008000">//</span><span style="color: #008000">myCat2鏄€氳繃榛樿鐨勬嫹璐濆嚱鏁版潵榪涜鍒濆鍖栫殑</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080"> 98</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">myCat2鐨勫湴鍧€鏄?nbsp;</span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">myCat2</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080"> 99</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">endl</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">---------------------------</span><span style="color: #000000">"</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080">100</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">101</span> <span style="color: #000000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">鐩存帴鐨勮皟鐢ㄩ粯璁ゆ嫹璐濇瀯閫犲嚱鏁扮殑<br /> </span><span style="color: #008080">102</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"鐩存帴鐨勮皟鐢ㄩ粯璁ゆ嫹璐濇瀯閫犲嚱鏁扮殑,榪欎釜璇彞涓€鍏卞垱寤轟簡澶氬皯涓璞?<<endl;<br /> </span><span style="color: #008080">103</span> <span style="color: #008000">    </span><span style="color: #008000">//</span><span style="color: #008000">SimpleCat myCat3 = TheFunctionTwo();</span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">榪欎釜灝界劧娌¤皟鐢ㄦ嫹璐濇瀯閫犲嚱鏁?????<br /> </span><span style="color: #008080">104</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"myCat3鐨勫湴鍧€鏄?nbsp;"<<&myCat3<<endl;<br /> </span><span style="color: #008080">105</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"myCat3.GetAge()  "<<myCat3.GetAge()<<endl;<br /> </span><span style="color: #008080">106</span> </strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">107</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">璋冪敤榛樿鐨勮祴鍊艱繍綆楃鐨?br /> </span><span style="color: #008080">108</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"璋冪敤榛樿鐨勮祴鍊艱繍綆楃鐨?榪欎釜璇彞涓€鍏卞垱寤轟簡澶氬皯涓璞?<<endl;<br /> </span><span style="color: #008080">109</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">SimpleCat myCat4;<br /> </span><span style="color: #008080">110</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">myCat4 = TheFunctionTwo();<br /> </span><span style="color: #008080">111</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"myCat4.GetAge()  "<<myCat4.GetAge()<<endl;<br /> </span><span style="color: #008080">112</span> </strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">113</span> </strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">114</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">榪欑璋冪敤鐨勬柟寮?br /> </span><span style="color: #008080">115</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"TheFunctionTwo()榪斿洖鐨勪復鏃跺璞¤祴緇欎竴涓紩鐢?<<endl;<br /> </span><span style="color: #008080">116</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">SimpleCat &rmyCat = TheFunctionTwo();<br /> </span><span style="color: #008080">117</span> <span style="color: #008000">    </span><span style="color: #008000">//</span></strong><strong><span style="color: #008000">cout<<"鎵撳嵃涓€涓嬭繑鍥炵殑涓存椂瀵硅薄鐨勫湴鍧€錛屼復鏃跺璞¤祴緇欏紩鐢ㄤ箣鍚庡氨涓嶄細绔嬪嵆鏋愭瀯浜?--"<<&rmyCat<<endl;<br /> </span><span style="color: #008080">118</span> <span style="color: #008000">    </span><span style="color: #008000">//</span><span style="color: #008000">cout<<"rmCat.GetAge() "<<rmyCat.GetAge()<<endl;</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">119</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">120</span> </strong><strong><span style="color: #000000">    SimpleCat myCat5;<br /> </span><span style="color: #008080">121</span> </strong><strong><span style="color: #000000">    TheFunctionFour(myCat5);<br /> </span><span style="color: #008080">122</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">123</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span></strong><strong><span style="color: #000000">;<br /> </span><span style="color: #008080">124</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080">125</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">126</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">127</span> <span style="color: #008000">//</span></strong><strong><span style="color: #008000">榪欎釜鍑芥暟鍦ㄨ繑鍥炴椂錛屾槸鍚︿細鍒涘緩涓€涓復鏃跺紩鐢ㄥ彉閲???<br /> </span><span style="color: #008080">128</span> <span style="color: #008000">//</span></strong><strong><span style="color: #008000">TheFunctionTwo(SimpleCat &simpleCat)榪欎釜鍑芥暟鍦ㄤ紶閫掑弬鏁版椂鏄惁浼氬垱寤轟竴涓復鏃剁殑寮曠敤鍙橀噺<br /> </span><span style="color: #008080">129</span> <span style="color: #008000">//</span><span style="color: #008000">涓存椂鐨勫紩鐢ㄥ彉閲忕殑浣滅敤鍩熻寖鍥存槸浠€涔?/span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">130</span> <span style="color: #000000">SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">TheFunction()<br /> </span><span style="color: #008080">131</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080">132</span> <span style="color: #000000">    SimpleCat </span><span style="color: #000000">*</span><span style="color: #000000">pFrisky </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SimpleCat(</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #000000">9</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080">133</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">pFrisky: </span><span style="color: #000000">"</span><span style="color: #000000"><<</span><span style="color: #000000">pFrisky</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080">134</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">*</span></strong><strong><span style="color: #000000">pFrisky;<br /> </span><span style="color: #008080">135</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080">136</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">137</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">138</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">139</span> <span style="color: #008000">//</span><span style="color: #008000">鎴戣鐪嬪璞¤鍒涘緩浜嗗嚑嬈?鏄惁鍒涘緩涓存椂瀵硅薄</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">140</span> </strong><strong><span style="color: #000000">SimpleCat TheFunctionTwo()<br /> </span><span style="color: #008080">141</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080">142</span> </strong><strong><span style="color: #000000">    SimpleCat tempCat;<br /> </span><span style="color: #008080">143</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">in TheFunctionTwo tempCat鎸囬拡 </span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">tempCat</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080">144</span> <span style="color: #000000">    tempCat.SetAge(</span><span style="color: #000000">9999</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080">145</span> <span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">榪斿洖鏃舵槸鍚﹁繕鍒涘緩涓€涓復鏃跺璞?/span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">146</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span></strong><strong><span style="color: #000000"> tempCat;<br /> </span><span style="color: #008080">147</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">148</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080">149</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">150</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">151</span> <span style="color: #008000">//</span><span style="color: #008000">榪欑鏂瑰紡鑲畾鏄敊鐨勶紝榪斿洖浜嗗眬閮ㄥ彉閲忕殑寮曠敤</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">152</span> <span style="color: #000000">SimpleCat </span><span style="color: #000000">&</span></strong><strong><span style="color: #000000">TheFunctionThree()<br /> </span><span style="color: #008080">153</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080">154</span> <span style="color: #000000">    SimpleCat tempCat;</span><span style="color: #008000">//</span><span style="color: #008000">灞€閮ㄥ彉閲?/span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">155</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">in TheFunctionThree tempCat鎸囬拡 </span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">tempCat</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080">156</span> <span style="color: #000000">    tempCat.SetAge(</span><span style="color: #000000">9999</span></strong><strong><span style="color: #000000">);<br /> </span><span style="color: #008080">157</span> <span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">榪斿洖鏃舵槸鍚﹁繕鍒涘緩涓€涓復鏃跺璞?宸插紩鐢ㄧ殑褰㈠紡</span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">158</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span></strong><strong><span style="color: #000000"> tempCat;<br /> </span><span style="color: #008080">159</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080">160</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">161</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">162</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">163</span> <span style="color: #008000">//</span><span style="color: #008000">鎴戣鐪嬪啀瀹炲弬瀵瑰艦鍙傝繘琛屾嫹璐濇瀯閫犳椂,鏄惁浼氭墦鍗板嚭璋冪敤鐨勬嫹璐濆嚱鏁頒俊鎭?/span></strong><span style="color: #008000"><br /> </span><strong><span style="color: #008080">164</span> <span style="color: #0000ff">void</span></strong><strong><span style="color: #000000"> TheFunctionFour(SimpleCat simpleCat)<br /> </span><span style="color: #008080">165</span> </strong><strong><span style="color: #000000">{<br /> </span><span style="color: #008080">166</span> <span style="color: #000000">    cout</span><span style="color: #000000"><<</span><span style="color: #000000">"</span><span style="color: #000000">褰㈠紡鍙傛暟simpleCat鐨勫湴鍧€ </span><span style="color: #000000">"</span><span style="color: #000000"><<&</span><span style="color: #000000">simpleCat</span><span style="color: #000000"><<</span></strong><strong><span style="color: #000000">endl;<br /> </span><span style="color: #008080">167</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">168</span> </strong><strong><span style="color: #000000">}<br /> </span><span style="color: #008080">169</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">170</span> </strong><span style="color: #000000"><br /> </span><strong><span style="color: #008080">171</span> </strong></div> <br /> <span style="color: #000000">------------------------------------------------------------------------------------------------------------------------------<br /> 鍏堣璇寸涓€鍧楀惂錛?<br /> 榪欎釜璨屼技搴旇璋冪敤鎷瘋礉鏋勯€犵殑鍦版柟娌℃湁璋冪敤鎷瘋礉鏋勯€狅紝搴旇鏄紪璇戝櫒鍋氱殑浼樺寲錛屽彲浠ュ弬鑰冦€婃繁鍏ユ帰绱++瀵硅薄妯″瀷銆婸66欏點€?<br /> 鎸夌収涔︿腑鐨勮娉曪紝寰堝彲鑳藉彧鍒涘緩浜?涓璞?<br />  <br /> X bar() <br /> { <br />     X xx; <br />     return xx; <br /> } <br /> 鍙兘浼氳緙栬瘧鍣ㄤ紭鍖栨垚 <br /> void bar(X & _result) <br /> { <br />     _result.X::X(); <br />     return; <br /> } <br />  <br /> 鎵€浠ヨ皟鐢╔ a = bar(),鍏跺疄琚漿鎹㈡垚 bar(X& a); <br /> 鎵€浠ヤ竴涓湰璇ヨ皟鐢ㄦ嫹璐濇瀯閫犵殑鍦版柟鍗村緢鍙兘璋冧粎鐢ㄤ簡鏋勯€犲嚱鏁版潵瀹屾垚宸ヤ綔銆?<br /> 榪欎釜闂鎴戜篃娌℃湁鎼炴槑鐧斤紝鎴戞病鏈夎瘯榪噇z鐨勪唬鐮佹槸涓嶆槸浼氳繖鏍鳳紝濡傛灉鐪熻繖鏍風殑璇濓紝鎴戣寰楃紪璇戝櫒灝辯鐨勫お澶氫簡錛屽鏋滄嫹璐濇瀯閫犱腑鏈変竴浜涚壒孌婄殑鍔熻兘鍛?灝卞儚妤間富鏈変釜杈撳嚭璇彞)錛屽矀涓嶆槸鏃犲0鏃犳伅涓鎶規潃浜嗐€傝繖涓姛鑳借縐頒負NRV錛屽ソ鍍忎竴鐩存病鏈変漢瀵硅繖涓棶棰樼粰鍑洪潪甯告槑紜殑鍥炵瓟 <br /> ------------------------------------------------------------------------------------------------------------------------------<br /> thx錛屽湪vs2005涓唬鐮佷紭鍖栧紑鍚椂錛岀涓€鍧楃‘瀹炲彧浼氳皟涓€涓瀯閫犲嚱鏁?<br /> 鎶婁唬鐮佷紭鍖栫鐢ㄥ悗錛屽氨浼氳皟鐢ㄦ嫹璐濇瀯閫犲嚱鏁頒簡 <br /> 紜疄鏄釜NRV鐨勯棶棰?<br /> http://blog.vckbase.com/bruceteen/archive/2005/12/30/16652.html <br /> ------------------------------------------------------------------------------------------------------------------------------<br /> <br /> <span style="color: #ff0000">姝e4妤肩殑鎵€璇磣~~ <br /> 鍒板簳鍒涘緩澶氬皯涓存椂瀵硅薄錛岃瑙嗙紪璇戝櫒鑰屽畾錛岀湅緙栬瘧鍣ㄦ槸鍚﹂噰鐢∟VR... <br /> (鍏跺疄涓€鑸儏鍐典笅錛孨VR閮芥槸鏈噰鐢ㄧ殑~),鍥犳瀵瑰悗涓変釜娉ㄩ噴浠g爜鍋?<br /> 濡備笅鍒嗘瀽錛氬啓鍑虹紪璇戝悗鐨勪吉浠g爜錛堜粎渚涘弬鑰? <br /> 緙栬瘧鍣ㄦ洿鏀瑰悗鐨勫嚱鏁板師鍨媣oid TheFunctionTwo(SimpleCat&) <br /> 娉ㄩ噴1錛?<br />     SimpleCat myCat3; <br />     TheFunctionTwo(myCat3) <br />    { <br />       SimpleCat tempCat;  <br />       templCat.SimpleCat::SimpleCat(); <br />       cout<<"in TheFunctionTwo tempCat鎸囬拡 "<<&tempCat<<endl;  <br />       tempCat.SetAge(9999); <br />       myCat3.SimpleCat::SimpleCat(tempCat); <br />       tempCat.SimpleCat::~SimpleCat(); <br />    } <br /> 娉ㄩ噴2錛?<br />     SimpleCat myCat4; <br />     myCat4.SimpleCat::Simple(); <br />     SimpleCat temp; <br />     TheFunctionTwo(temp) <br />    { <br />      SimpleCat tempCat;  <br />       templCat.SimpleCat::SimpleCat(); <br />       cout<<"in TheFunctionTwo tempCat鎸囬拡 "<<&tempCat<<endl;  <br />       tempCat.SetAge(9999); <br />       temp.SimpleCat::SimpleCat(tempCat); <br />       tempCat.SimpleCat::~SimpleCat(); <br />    } <br />     myCat4.operator=(temp); <br />    temp.SimpleCat::~SimpleCat(); <br /> 娉ㄩ噴3錛?nbsp; <br />     //SimpleCat &rmyCat = TheFunctionTwo();  <br />      SimpleCat temp; <br />      SimpleCat &rmyCat=temp; <br />     TheFunctionTwo(temp) <br />     { <br />       SimpleCat tempCat;  <br />       templCat.SimpleCat::SimpleCat(); <br />       cout<<"in TheFunctionTwo tempCat鎸囬拡 "<<&tempCat<<endl;  <br />       tempCat.SetAge(9999); <br />       temp.SimpleCat::SimpleCat(tempCat); <br />       tempCat.SimpleCat::~SimpleCat(); <br />    } <br /> 榪欐牱灝辯煡閬撳埌搴曢渶瑕佸灝戜復鏃跺璞′簡~~~~ </span><br /> </span></span> <img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/270051.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-11 15:22 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270051.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浠嶫ava鍒癈++ 鈥?瀵規瘮Java涓嶤++緙栫▼鐨勪笉鍚?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270049.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Mon, 11 May 2009 06:58:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270049.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/270049.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270049.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270049.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/270049.html</trackback:ping><description><![CDATA[  <h2><a title="Permanent Link to 浠嶫ava鍒癈++ 鈥?瀵規瘮Java涓嶤++緙栫▼鐨勪笉鍚? >浠嶫ava鍒癈++ 鈥?瀵規瘮Java涓嶤++緙栫▼鐨勪笉鍚?/a></h2> <p>14th August 2007, 11:40 pm </p> <p>鍘熶綔錛?a target="_blank">Cay Horstmann</a> <a target="_blank">鑻辨枃鍘熸枃</a></p> <p>緲昏瘧錛欰qua  <a title="鍏ㄦ枃閾炬帴" >prglab.com</a></p> <p>娉細寰堝紼嬪簭鍛樺寘鎷湰浜哄湪鍐呴兘鏄厛瀛︿細鐨凧ava錛岀劧鍚庢墠瀛︾殑C++錛屽叾瀹濩++涓嶫ava鏈夊緢澶氱浉浼煎拰浜掗€氫箣澶勶紝鏈夌浉褰撶殑Java鐭ヨ瘑鍙互瀵瑰簲杞寲鍒癈++姒傚康錛屼粠鑰屽府鍔╂垜浠揩閫熶笂鎵嬨€傝繖綃囨枃绔犱粙緇嶇殑鍐呭灝卞浠嶫ava鍚慍++杞彉寰堟湁甯姪錛屾墍浠ョ炕璇戞帹鑽愮粰鏈夊悓鏍烽渶瑕佺殑鏈嬪弸銆傜炕璇戜腑鍔犲叆浜嗘湰浜虹殑鐞嗚В錛屼笉瀹屽叏鏄叏鏂囩収鎼€傛湁涓嶆槑鎴栧紓璁紝璇峰弬鑰冨師鏂囷紝鎴栫暀璦€璁ㄨ銆傝繕鏄偅鍙ヨ瘽錛屾帹鑽愬璇誨師鐗堣祫鏂欍€?/p> <p>瀛︿範鎬庢牱浠庝竴縐嶇紪紼嬭璦€杞Щ鍒板彟涓€縐嶇紪紼嬭璦€鏄粖鏃ョ殑紼嬪簭鍛樺繀欏婚潰瀵圭殑鐜板疄錛岃繕濂斤紝C++鍜孞ava鏈夊緢澶氬叡鍚岀殑鐗圭偣錛屾墍浠ヤ粠Java杞埌C++灝卞鏄撹澶氥€侰++姣擩ava澶嶆潅寰堝錛屾湰鏂囧茍娌℃墦綆楁兜鐩栨墍鏈塁++鐨勫姛鑳姐€備絾鏄鏋滀綘鑳藉鐔熺粌鎺屾彙鏈枃涓殑鎵€鏈夌煡璇嗙偣錛屼篃瓚沖鏈夋晥鐨勪嬌鐢–++浜嗐€?/p> <p>榪欓噷鎴戜滑鍙Java涓嶤++鐨勪笉鍚屼箣澶勩€傚儚嫻佺▼鎺у埗(if, while, for)榪欎簺鍦–++涓嶫ava涓畬鍏ㄤ竴鏍風殑鍐呭榪欓噷灝變笉璁蹭簡銆?/p> <p>鏈枃鏄熀浜嶢NSI C++鏍囧噯鐨勶紝涓€浜涜€佺殑C++ 緙栬瘧鍣ㄥ彲鑳戒笉鏀寔榪欓噷璁插埌鐨勪竴浜涢噸瑕佸姛鑳姐€傝浣跨敤榪欎簺緙栬瘧鍣紝浣犻渶瑕佹洿澶氬涔燙++涓粠C緇ф壙鏉ョ殑閮ㄥ垎銆傞偅浜涢兘瓚呭嚭浜嗘湰鏂囩殑鑼冪暣錛屼篃灝變笉浠嬬粛浜嗐€?/p> <h2>1. 鏁版嵁綾誨瀷鍜屽彉閲?/h2> <p>C++ 涓殑鍙橀噺綾誨瀷涓嶫ava寰堢浉浼箋€傚儚Java涓€鏍鳳紝C++ 鏈?tt><span style="font-size: 12pt; line-height: 150%">int</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">double</span></tt>綾誨瀷<strong><span style="color: red">銆備絾鏄繖浜涙暟瀛楃被鍨嬬殑鍙栧€艱寖鍥存槸渚濊禆浜庢満鍣ㄧ殑銆?/span></strong>姣斿鍦?6浣嶇郴緇熶笂錛屼緥濡傝繍琛孌OS 鎴朩indows 3.x鐨凱C鏈轟笂錛?tt><span style="font-size: 12pt; line-height: 150%">int</span></tt>鏄弻瀛楄妭(2-byte)鐨勶紝鍙栧€艱寖鍥存瘮Java鐨?-byte鐨?tt><span style="font-size: 12pt; line-height: 150%">int</span></tt>瑕佸皬寰堝銆傚湪榪欎簺鏈哄櫒涓婏紝濡傛灉 int 涓嶅鐢ㄧ殑璇濓紝浣犻渶瑕佷嬌鐢ㄩ暱鏁村瀷long銆?/p> <p>C++ 鏈?<tt><span style="font-size: 12pt; line-height: 150%">short</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">unsigned</span></tt>綾誨瀷鏉ユ洿鏈夋晥鐨勫瓨鍌ㄦ暟瀛椼€傦紙鎴戣涓烘墍璋撴湁鏁堟槸鎸囨洿楂樼殑絀洪棿鍒╃敤鐜囥€傦級 鏈€濂芥槸灝介噺閬垮厤浣跨敤榪欎簺綾誨瀷闄ら潪鏄┖闂村埄鐢ㄧ殑鏈夋晥鎬у浣犵殑緋葷粺鐪熺殑闈炲父閲嶈銆?/p> <p><strong><span style="color: red">鍦–++涓竷灝斿瀷鐢?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">bool</span></strong></tt><strong><span style="color: red">琛ㄧず錛岃€屼笉鍍忓湪Java涓敤boolean銆?/span></strong></p> <p>C++ 涓瓧絎︿覆綾誨瀷鐢?<tt><span style="font-size: 12pt; line-height: 150%">string</span></tt>琛ㄧず銆傚畠涓嶫ava涓殑 <tt><span style="font-size: 12pt; line-height: 150%">String</span></tt>綾誨瀷闈炲父鐩鎬技錛屼絾鏄紝榪樻槸瑕侀€愪竴浠ヤ笅鍑犵偣涓嶅悓涔嬪錛?/p> <p>1. C++ 瀛楃涓插瓨鍌ˋSCII 鐮佸瓧絎︼紝鑰屼笉鏄爣鍑嗙爜Unicode 瀛楃</p> <p>2. C++ 瀛楃涓叉槸鍙互琚慨鏀圭殑錛岃€孞ava瀛楃涓茬殑鍐呭鏄笉鍙慨鏀圭殑(immutable)銆?/p> <p>3. 鍙栧瓙瀛楃涓茬殑鎿嶄綔鍦?C++ 涓彨鍋?<tt><span style="font-size: 12pt; line-height: 150%">substr</span></tt>錛岃繖涓懡浠?tt><span style="font-size: 12pt; line-height: 150%">s.substr(i, n)</span></tt>浠庡瓧絎︿覆s涓彇寰椾粠浣嶇疆 i 寮€濮嬮暱搴︿負n鐨勫瓙瀛楃涓層€?/p> <p>4. <strong><span style="color: red">鍦–++涓紝浣犲彧鑳藉灝嗗瓧絎︿覆涓庡叾瀹冨瓧絎︿覆瀵硅薄鐩鎬覆鑱?concatenate)錛岃€屼笉鑳藉涓庝換鎰忕殑瀵硅薄鐩鎬覆鑱斻€?/span></strong></p> <p>5.<strong><span style="color: red"> C++</strong></span><strong><span style="color: red">涓彲浠ョ洿鎺ヤ嬌鐢ㄥ叧緋繪搷浣滅 </span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">==</span></strong></tt><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">銆?!=銆?<銆?<=銆?>銆?>= </span></strong></tt><strong><span style="color: red">鏉ヨ繘琛屽瓧絎︿覆姣旇緝錛?/span></strong>鍏朵腑鍚庨潰鍥涗釜鎿嶄綔絎︽槸鎸夊瓧姣嶉『搴忚繘琛屾瘮杈冪殑銆傝繖姣擩ava涓嬌鐢ㄥ嚱鏁癳quals鍜宑ompareTo鏉ユ瘮杈冭鏂逛究寰堝銆?/p> <h2>2. 鍙橀噺鍜屽父閲?/h2> <p>鍦–++涓紝鏈湴鍙橀噺鐨勫畾涔夌湅璧鋒潵涓嶫ava涓浉鍚岋紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">int n = 5;</span></tt></p> <p><strong><span style="color: red">瀹為檯涓婅繖姝f槸C++鍜孞ava鐨勪竴涓噸瑕佷笉鍚屼箣澶勩€侰++緙栬瘧鍣ㄤ笉瀵規湰鍦板彉閲忚繘琛屽垵濮嬪寲媯€楠岋紝鎵€浠ュ湪C++涓緢瀹規槗蹇樿鍒濆鍖栦竴涓彉閲忥紝榪欑鎯呭喌涓嬶紝鍙橀噺鐨勫€艱鍙橀噺鎵€鍗犲唴瀛樺尯鍩熶腑鍒氬ソ褰撳墠瀛樺湪闅忔満鍊箋€傝繖鏄劇劧鏄緢瀹規槗浜х敓紼嬪簭鍑洪敊鐨勫湴鏂廣€?/span></strong></p> <p>涓嶫ava涓€鏍鳳紝 C++涓被鍙互鏈夋暟鎹煙鍜岄潤鎬佸彉閲忋€備笉鍚岀殑鏄紝C++涓彉閲忓彲浠ュ湪鍑芥暟鐢氳嚦鏄被鐨勫闈㈠畾涔夛紝榪欎簺鎵€璋撶殑<strong><span style="color: red">鍏ㄥ眬鍙橀噺</span></strong>鍙互鍦ㄧ▼搴忕殑浠諱綍鍑芥暟涓璁塊棶錛屽洜鑰屼笉鏄撹寰堝ソ鐨勭鐞嗐€?strong><span style="color: red">鎵€C++涓簲璇ュ敖閲忛伩鍏嶄嬌鐢ㄥ叏灞€鍙橀噺銆?/span></strong></p> <p>鍦–++涓紝甯擱噺鍙互鍦ㄤ換浣曞湴鏂硅瀹氫箟錛堣寰楀湪Java涓紝甯擱噺蹇呴』鏄被鐨勯潤鎬佹暟鎹畇tatic data)銆?strong><span style="color: red"> C++ </span><span style="color: red">浣跨敤鍏抽敭瀛?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">const</span></strong></tt><strong><span style="color: red">鏉ュ畾涔夊父閲忥紝鑰孞ava涓槸 </span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">final</span></strong></tt><strong><span style="color: red">銆備緥濡傦細</span></strong></p> <p><tt><span style="font-size: 12pt; line-height: 150%">const int DAYS_PER_YEAR = 365;</span></tt></p> <h2>3. 綾?/h2> <p>C++ 涓綾葷殑瀹氫箟涓嶫ava鏈変簺涓嶅悓錛岃繖閲屾槸涓€涓緥瀛愶細涓€涓狢++ 鐗堟湰鐨?<tt><span style="font-size: 12pt; line-height: 150%">Point</span></tt>綾?</p> <p><tt><span style="font-size: 12pt; line-height: 150%">class Point /* C++ */</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">public:</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Point();</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Point(double xval, double yval);</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">void move(double dx, double dy);</span></tt></p> <p><tt><span style="font-size: 12pt; color: blue; line-height: 150%">double getX() <strong>const;</strong></span></tt></p> <p><tt><span style="font-size: 12pt; color: blue; line-height: 150%">double getY() <strong>const;</strong></span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">private:</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">double x;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">double y;</span></tt></p> <p><tt><strong><span style="font-size: 14pt; color: red; line-height: 150%">};</span></strong></tt></p> <p>榪欓噷鍑犵偣閲嶈鐨勪笉鍚屾槸錛?/p> <p>1. C++鐨勭被瀹氫箟涓垎涓哄叕鍏卞拰縐佹湁閮ㄥ垎錛屽垎鍒互鍏抽敭瀛?<tt><span style="font-size: 12pt; line-height: 150%">public</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">private</span></tt>寮€濮嬨€傝€屽湪Java涓紝姣忎竴涓厓绱犻兘蹇呴』鏍囨槑 <tt><span style="font-size: 12pt; line-height: 150%">public</span></tt>鎴?<tt><span style="font-size: 12pt; line-height: 150%">private</span></tt>銆?/p> <p>2.<strong><span style="color: red"> C++</strong></span><strong><span style="color: red">涓被鐨勫畾涔夊彧鍖呭惈鍑芥暟鐨勫0鏄庯紝鐪熸鐨勫疄鐜板彟澶栧崟鐙垪鍑恒€?/span></strong></p> <p><strong><span style="color: blue">3. </span><span style="color: blue">璁塊棶鍑芥暟(accessor methods)鏍囨湁鍏抽敭瀛?</span></strong><tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">const</span></strong></tt><strong><span style="color: blue">錛岃〃鏄庤繖涓嚱鏁頒笉浼氭敼鍙樻湰瀵硅薄鐨勫厓绱犲€箋€?/span></strong></p> <p><strong><span style="color: red">4. </span><span style="color: red">綾誨畾涔夌殑緇撳熬澶勬湁鍒嗗彿</span></strong></p> <p>綾諱腑鍑芥暟鐨勫疄鐜拌窡鍦ㄧ被鐨勫畾涔変箣鍚庛€傚洜涓哄嚱鏁版槸鍦ㄧ被澶栭潰瀹氫箟鐨勶紝鎵€浠ユ瘡涓€涓嚱鏁扮殑鍚嶅瓧鍓嶉潰瑕佸姞綾誨悕縐頒綔涓哄墠緙€錛屽茍浣跨敤<strong><span style="color: red">鎿嶄綔絎﹀弻鍐掑彿::鏉ュ垎鍓茬被鐨勫悕縐板拰鍑芥暟鐨勫悕縐?/span></strong>銆備笉鏀瑰彉闅愬惈鍙傛暟鍊鹼紙鍗沖綋鍓嶅璞$殑鍊鹼級鐨勮闂嚱鏁扮敤 <tt><span style="font-size: 12pt; line-height: 150%">const</span></tt>鏍囨槑銆傚涓嬫墍紺烘槸涓婇潰綾誨畾涔変腑鐨勫嚱鏁扮殑瀹炵幇錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">Point::Point() { x = 0; y = 0; }</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">void Point::move(double dx, double dy) </span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">x = x + dx;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">y = y + dy;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">double Point::getX() const</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ </span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">return x;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <h2>4. 瀵硅薄</h2> <p><strong><span style="font-size: 14pt; color: red; line-height: 150%">Java </span></strong><strong><span style="font-size: 14pt; color: red; line-height: 150%">涓?C++ 鏈€涓昏鐨勪笉鍚屽湪浜庡璞″彉閲忕殑浣跨敤銆?/span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">鍦?C++涓紝瀵硅薄鍙橀噺瀛樺偍鐨勬槸鐪熸鐨勫璞$殑鍊鹼紝鑰屼笉鏄璞″紩鐢?<a title="鍏充簬reference鐨勬枃绔? >reference</a>)銆傛敞鎰忓湪C++涓瀯閫犱竴涓璞$殑鏃跺€欐槸涓嶄嬌鐢ㄥ叧閿瓧new鐨勶紝鍙渶瑕佸湪鍙橀噺鐨勫悕瀛楀悗闈㈢洿鎺ヨ祴浜堟瀯閫犲嚱鏁扮殑鍙傛暟灝卞彲浠ヤ簡錛屼緥濡傦細</span></strong></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Point p(1, 2); /* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">鏋勯€犲璞?p */</span></tt></p> <p>濡傛灉涓嶈窡鍙傛暟璧嬪€鹼紝鍒欎嬌鐢ㄩ粯璁ゆ瀯閫犲嚱鏁幫紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">Time now; /* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">榛樿浣跨敤鏋勯€犲嚱鏁?Time::Time() */</span></tt></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">榪欎竴鐐逛笌Java寰堜笉鍚屻€傚湪Java涓紝榪欎釜鍛戒護浠呬粎鐢熸垚涓€涓病鏈夊垵濮嬪寲鐨勫璞eference錛岃鍒涘緩瀵硅薄榪樺緱浣跨敤new鍦ㄥ爢涓垱寤轟竴涓疄闄呯殑瀵硅薄.   </span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">鑰屽湪C++涓紝瀹冪敓鎴愪竴涓疄闄呯殑瀵硅薄銆?/span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">褰撲竴涓璞¤璧嬬粰鍙︿竴涓璞″彉閲忕殑鏃跺€欙紝瀹為檯鐨勫€煎皢琚嫹璐濄€傝€屽湪Java涓紝鎷瘋礉涓€涓璞″彉閲忓彧涓嶈繃鏄緩绔嬩簡鍙﹀涓€涓寚鍚戝璞$殑reference銆傛嫹璐濅竴涓狢++鐨勫璞″氨鍍忓湪Java涓皟鐢╟lone榪欎釜鍑芥暟涓€鏍鳳紝鑰屼慨鏀規嫹璐濈殑鍊間笉浼氭敼鍙樺師瀵硅薄鐨勫€箋€?/span></strong>渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">Point q = p; /* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">鎷瘋礉p鍒皅 ,q鏄彟澶栦竴涓柊鐨勫璞★紝鏈夎嚜宸辯殑鍐呭瓨絀洪棿*/</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">q.move(1, 1); /* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">縐誨姩q鑰宲涓嶅姩錛屽嵆q鐨勫€煎彉浜嗭紝鑰宲鐨勪笉鍙?/</span></tt></p> <p>澶氭暟鎯呭喌涓嬶紝C++涓繖縐嶅璞$洿鎺ュ鍊兼搷浣滅殑鐗規€т嬌鐢ㄨ搗鏉ュ緢鏂逛究錛屼絾鏄篃鏈変簺鏃跺€欎笉灝藉浜烘剰錛?/p> <p><strong><span style="font-size: 14pt; color: blue; line-height: 150%">C++</span></strong><strong><span style="font-size: 14pt; color: blue; line-height: 150%">涓璞$殑鎸囬拡闈炲父綾諱技浜嶫ava涓殑寮曠敤</span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">1. </span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">褰撻渶瑕佷竴涓嚱鏁頒腑淇敼涓€涓璞$殑鍊鹼紝蹇呴』璁頒綇瑕佷嬌鐢ㄦ寜寮曠敤璋冪敤call by reference (鍙傝涓嬮潰鍑芥暟閮ㄥ垎)錛屾垨鑰呭皢鍙傛暟瀹氫箟涓烘寚閽堢被鍨?鍦ㄥ嚱鏁頒腑閫氳繃鎸囬拡鏉ユ搷浣滃叾</span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">瀹為檯鎸囧悜鐨勫璞?/span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">2. </span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">涓や釜瀵硅薄鍙橀噺涓嶈兘鎸囧悜鍚屼竴涓璞″疄浣撱€傚鏋滀綘瑕佸湪C++涓疄鐜拌繖縐嶆晥鏋滐紝蹇呴』浣跨敤鎸囬拡pointer錛堝弬瑙佷笅闈㈡寚閽堥儴鍒嗭級</span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">3. </span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">涓€涓璞″彉閲忓彧鑳藉瓨鍌ㄤ竴縐嶇壒瀹氱殑綾誨瀷鐨勫€鹼紝濡傛灉浣犳兂瑕佷嬌鐢ㄤ竴涓彉閲忔潵瀛樺偍涓嶅悓瀛愮被鐨勫璞$殑鍊鹼紙澶氭€乸loymorphism)錛屽垯闇€瑕佷嬌鐢ㄦ寚閽堛€?/span></strong></p> <p><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">4. </span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">濡傛灉浣犳兂鍦–++涓嬌鐢ㄤ竴涓彉閲忔潵鎴栬€呮寚鍚憂ull鎴栬€呮寚鍚戜竴涓疄闄呯殑瀵硅薄錛屽垯闇€瑕佷嬌鐢ㄦ寚閽?/span></strong></p> <h2>5. 鍑芥暟</h2> <p>鍦↗ava涓紝姣忎竴涓嚱鏁板繀欏繪垨鑰呮槸瀵硅薄鍑芥暟(instance method)錛屾垨鑰呮槸闈欐€佸嚱鏁?static function)鎴栫О綾誨嚱鏁般€侰++鍚屾牱鏀寔瀵硅薄鍑芥暟鍜岄潤鎬佸嚱鏁幫紙綾誨嚱鏁幫級錛屼絾鍚屾椂C++涔熷厑璁稿畾涔変笉灞炰簬浠諱綍綾葷殑鍑芥暟錛岃繖浜涘嚱鏁板彨鍋?strong><span style="color: blue">鍏ㄥ眬鍑芥暟<em><span style="font-family: 瀹嬩綋">錛坓lobal functions錛?/span></em>銆?/span></strong></p> <p>鐗瑰埆鐨勬槸錛屾瘡涓€涓狢++ 紼嬪簭閮戒粠涓€涓彨鍋?<tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">main</span></strong></tt><strong><span style="color: blue">鐨勫叏灞€鍑芥暟</span></strong>寮€濮嬫墽琛岋細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">int main()</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ . . .</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p>榪樻湁鍙﹀涓€涓牸寮忕殑main鍑芥暟鍙互鐢ㄦ潵鎹曟崏鍛戒護琛屽弬鏁幫紝綾諱技浜嶫ava鐨刴ain鍑芥暟錛屼絾鏄畠瑕佹眰鍏充簬C鏍煎紡鐨勬暟緇勫拰瀛楃涓茬殑鐭ヨ瘑錛岃繖閲屽氨涓嶄粙緇嶄簡銆?/p> <p>鎸夌収涔犳儻錛岄€氬父濡傛灉紼嬪簭鎵ц鎴愬姛錛?<tt><span style="font-size: 12pt; line-height: 150%">main</span></tt>鍑芥暟榪斿洖0錛屽惁鍒欒繑鍥為潪闆舵暣鏁般€?/p> <p><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">鍚孞ava涓€鏍鳳紝鍑芥暟鍙傛暟鏄€氳繃鍊間紶閫掔殑(passed by value)銆傚湪Java涓紝鍑芥暟鏃犺濡備綍閮芥槸鍙互淇敼瀵硅薄(綾誨璞?鐨勫€肩殑錛?/span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">鍥犱負java涓紶閫掔殑鏄痡ava涓殑寮曠敤,鐩稿綋涓嶤++涓殑鎸囬拡銆侸ava鍑芥暟榪斿洖鏃朵篃鏄繑鍥炵殑寮曠敤,灝嗘寮曠敤</span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">鎷瘋礉</span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">鍒板闈㈢殑鏌愪釜寮曠敤鍙橀噺涓?/span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">錛夈€傜劧鑰屽湪C++涓紝</span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">鍥犱負瀵硅薄鐩存帴瀛樺偍鐨勬槸瀹為檯鐨勫€?/span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">錛?/span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">鑰屼笉鏄寚鍚戝€肩殑reference錛?/span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">涔熷氨鏄浼犲叆鍑芥暟鐨勬槸涓€涓疄闄呭€肩殑鎷瘋礉錛屽洜姝や篃灝辨棤娉曚慨鏀瑰師鏉ュ璞$殑鍊箋€?C++涓嚱鏁拌繑鍥炴椂涔熸槸瑕佹嫹璐濆埌澶栭潰鐨勫彉閲忎腑鐨?鏄寚閽堝氨鎷瘋礉鎸囬拡錛屾槸瀵硅薄灝辮繘琛屽璞$殑鎷瘋礉)</span></strong></p> <p>鎵€浠ワ紝C++ 鏈変袱縐嶅弬鏁頒紶閫掓満鍒訛紝鍚孞ava涓€鏍風殑鎸夊€艱皟鐢?<em><span style="font-family: 瀹嬩綋">call by value) </em></span>錛屼互鍙婃寜鍦板潃璋冪敤(<em><span style="font-family: 瀹嬩綋">call by reference</em>)</span>銆傚綋涓€涓弬鏁版槸鎸塺eference浼犻€掓椂錛屽嚱鏁板彲浠ヤ慨鏀瑰叾鍘熷鍊箋€侰all by reference 鐨勫弬鏁板墠闈㈡湁涓€涓湴鍧€鍙?<tt><span style="font-size: 12pt; line-height: 150%">&</span></tt>璺熷湪鍙傛暟綾誨瀷鐨勫悗闈紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">void raiseSalary(Employee& e, double by)</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ . . .</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p><strong><span style="color: red">涓嬮潰鏄竴涓吀鍨嬬殑鍒╃敤call by reference鐨勫嚱鏁幫紝</span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">鍦↗ava涓槸鏃犳硶瀹炵幇榪欐牱鐨勫姛鑳界殑</span><span style="color: red">銆?/span></strong></p> <p><tt><span style="font-size: 12pt; line-height: 150%">void swap(int& a, int& b)</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ int temp = a;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">a = b;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">b = temp;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p>濡傛灉浣跨敤 <tt><span style="font-size: 12pt; line-height: 150%">swap(x, y)</span></tt>鏉ヨ皟鐢ㄨ繖涓嚱鏁幫紝鍒檙eference鍙傛暟 <tt><span style="font-size: 12pt; line-height: 150%">a</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">b</span></tt>鎸囧悜鍘熷疄闄呭弬鏁?tt><span style="font-size: 12pt; line-height: 150%">x</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">y</span></tt>鐨勪綅緗紝鑰屼笉鏄畠浠殑鍊肩殑鎷瘋礉錛屽洜姝よ繖涓嚱鏁板彲浠ュ疄鐜板疄闄呬氦鎹㈣繖涓や釜鍙傛暟鐨勫€箋€?/p> <p><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">鍦?C++涓紝姣忓綋闇€瑕佸疄鐜頒慨鏀瑰師鍙傛暟鐨勫€兼椂浣犲氨鍙互浣跨敤鎸夊湴鍧€璋冪敤 call by reference 銆?/span></strong></p> <p><strong> </strong></p> <p><strong> </strong></p> <p><strong> </strong></p> <h2>6. 鍚戦噺Vector</h2> <p>C++ 鐨勫悜閲忕粨鏋勭粨鍚堜簡Java涓暟緇勫拰鍚戦噺涓よ€呯殑浼樼偣銆備竴涓狢++ 鐨勫悜閲忓彲浠ユ柟渚跨殑琚闂紝鍏跺閲忓張鍙互鍔ㄦ€佺殑澧為暱銆傚鏋?<tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>鏄換鎰忕被鍨嬶紝鍒?<tt><span style="font-size: 12pt; line-height: 150%">vector<T></span></tt>鏄竴涓厓绱犱負 <tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>綾誨瀷鐨勫姩鎬佹暟緇勩€備笅闈㈢殑璇彞</p> <p><tt><span style="font-size: 12pt; line-height: 150%">vector<int> a;</span></tt></p> <p>浜х敓涓€涓垵濮嬩負絀虹殑鍚戦噺銆傝€岃鍙?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">vector<int> a(100);</span></tt></p> <p>鐢熸垚涓€涓垵濮嬫湁100涓厓绱犵殑鍚戦噺銆備綘鍙互浣跨敤<tt><span style="font-size: 12pt; line-height: 150%">push_back</span></tt>鍑芥暟鏉ユ坊鍔犲厓绱狅細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">a.push_back(n);</span></tt></p> <p>璋冪敤 <tt><span style="font-size: 12pt; line-height: 150%">a.pop_back()</span></tt>浠?tt><span style="font-size: 12pt; line-height: 150%">a</span></tt>涓彇鍑烘渶鍚庝竴涓厓绱狅紙鎿嶄綔鍚庤繖涓厓绱犺浠巃涓垹鎺?錛?浣跨敤鍑芥暟<tt><span style="font-size: 12pt; line-height: 150%">size</span></tt>鍙互寰楀埌褰撳墠a涓殑鍏冪礌涓暟銆?/p> <p>浣犺繕鍙互閫氳繃鎴戜滑鐔熸倝鐨?<tt><span style="font-size: 12pt; line-height: 150%">[]</span></tt>鎿嶄綔絎︽潵璁塊棶鍚戦噺涓厓绱狅紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">for (i = 0; i < a.size(); i++) {</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">sum = sum + a[i];</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p>鍚孞ava涓竴鏍鳳紝鏁扮粍绱㈠紩蹇呴』涓?0 鍜?tt><span style="font-size: 12pt; line-height: 150%">a.size() - 1</span></tt>涔嬮棿鐨勫€箋€備絾鏄笌Java涓嶅悓鐨勬槸錛孋++涓病鏈塺untime鐨勭儲寮曞彿鍚堟硶鎬ф楠屻€傝瘯鍥捐闂潪娉曠殑绱㈠紩浣嶇疆鍙兘閫犳垚闈炲父涓ラ噸鐨勫嚭閿欍€?/p> <p>灝卞儚鎵€鏈夊叾瀹?C++ 瀵硅薄涓€鏍鳳紝鍚戦噺涔熸槸鍊箋€傚鏋滀綘灝嗕竴涓悜閲忚祴鍊肩粰鍙﹀涓€涓悜閲忓彉閲忥紝鎵€鏈夌殑鍏冪礌閮戒細琚嫹璐濊繃鍘匯€?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">vector<int> b = a; /* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">鎵€鏈夌殑鍏冪礌閮借鎷瘋礉浜?*/</span></tt></p> <p>瀵規瘮Java涓殑鎯呭喌錛屽湪Java涓紝涓€涓暟緇勫彉閲忔槸涓€涓寚鍚戞暟緇勭殑reference銆傛嫹璐濊繖涓彉閲忎粎浠呬駭鐢熷彟澶栦竴涓寚鍚戝悓涓€鏁扮粍鐨剅eference錛岃€屼笉浼氭嫹璐濇瘡涓€涓厓绱犵殑鍊箋€?/p> <p>姝e洜濡傛錛屽鏋滀竴涓狢++鍑芥暟瑕佸疄鐜頒慨鏀瑰悜閲忕殑鍊鹼紝蹇呴』浣跨敤reference鍙傛暟錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">void sort(vector<int>& a)</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ . . .</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <h2>7. 杈撳叆鍜岃緭鍑?/h2> <p>鍦–++涓紝鏍囧噯鐨勮緭鍏ヨ緭鍑烘祦鐢ㄥ璞?<tt><span style="font-size: 12pt; line-height: 150%">cin</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">cout</span></tt>琛ㄧず銆傛垜浠嬌鐢?<tt><span style="font-size: 12pt; line-height: 150%"><<</span></tt>鎿嶄綔絎﹀啓杈撳嚭錛屼緥濡傦細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">cout << “Hello, World!”;</span></tt></p> <p>涔熷彲浠ヨ繛鐫€杈撳嚭澶氶」鍐呭錛屼緥濡傦細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">cout << “The answer is ” << x << “"n”;</span></tt></p> <p>鎴戜滑浣跨敤 <tt><span style="font-size: 12pt; line-height: 150%">>></span></tt>鎿嶄綔絎︽潵璇誨叆涓€涓暟瀛楁垨鍗曡瘝錛屼緥濡傦細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">double x;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">cout << “Please enter x: “;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">cin >> x;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">string fname;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">cout << “Please enter your first name: “;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">cin >> fname;</span></tt></p> <p>鍑芥暟<tt><span style="font-size: 12pt; line-height: 150%">getline</span></tt>鍙互璇誨叆鏁磋鐨勮緭鍏ワ紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">string inputLine;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">getline(cin, inputLine);</span></tt></p> <p>濡傛灉鍒拌揪杈撳叆鐨勭粨灝撅紝鎴栬€呬竴涓暟瀛楁棤娉曡姝g‘鐨勮鍏ワ紝榪欎釜嫻佸璞′細琚緗負 failed 鐘舵€侊紝鎴戜滑鍙互浣跨敤鍑芥暟 <tt><span style="font-size: 12pt; line-height: 150%">fail</span></tt>鏉ユ楠岃繖涓姸鎬侊紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">int n;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">cin >> n;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">if (cin.fail()) cout << “Bad input”;</span></tt></p> <p>涓€鏃︿竴涓祦鐨勭姸鎬佽璁句負failed錛屾垜浠槸寰堥毦閲嶇疆瀹冪殑鐘舵€佺殑錛屾墍浠ュ鏋滀綘鐨勭▼搴忛渶瑕佸鐞嗛敊璇緭鍏ョ殑鎯呭喌錛屽簲璇ヤ嬌鐢ㄥ嚱鏁?<tt><span style="font-size: 12pt; line-height: 150%">getline</span></tt>鐒跺悗浜哄伐澶勭悊寰楀埌鐨勮緭鍏ユ暟鎹€?/p> <h2>8. 鎸囬拡pointer</h2> <p><strong><span style="color: red">鎴戜滑宸茬粡鐭ラ亾鍦–++涓紝瀵硅薄鍙橀噺鐩存帴瀛樺偍鐨勬槸瀵硅薄鐨勫€箋€傝繖鏄笌Java涓嶅悓鐨勶紝鍦↗ava涓璞″彉閲忓瓨鍌ㄧ殑鏄竴涓湴鍧€錛岃鍦板潃鎸囧悜瀵硅薄鍊煎疄闄呭瓨鍌ㄧ殑鍦版柟銆傛湁鏃跺湪C++涓篃闇€瑕佸疄鐜拌繖鏍風殑甯冪疆錛岃繖灝辯敤鍒頒簡鎸囬拡pointer銆傚湪 C++涓紝涓€涓寚鍚戝璞$殑鍙橀噺鍙仛鎸囬拡銆傚鏋淭鏄竴縐嶆暟鎹被鍨嬶紝鍒?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">T*</span></strong></tt><strong><span style="color: red">鏄寚鍚戣繖縐嶆暟鎹被鍨嬬殑鎸囬拡銆?/span></strong></p> <p>灝卞儚 Java涓竴鏍鳳紝涓€涓寚閽堝彉閲忓彲浠ヨ鍒濆鍖栦負絀哄€?<tt><span style="font-size: 12pt; line-height: 150%">NULL</span></tt>錛屽彟澶栦竴涓寚閽堝彉閲忕殑鍊鹼紝鎴栬€呬竴涓皟鐢╪ew鐢熸垚鐨勬柊瀵硅薄錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* p = NULL;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* q = new Employee(”Hacker, Harry”, 35000);</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* r = q;</span></tt></p> <p><strong><span style="color: red">瀹為檯涓婂湪C++涓繕鏈夌鍥涚鍙兘錛岄偅灝辨槸鎸囬拡鍙互琚垵濮嬪寲涓哄彟澶栦竴涓璞$殑鍦板潃錛岃繖闇€瑕佷嬌鐢ㄥ湴鍧€鎿嶄綔絎?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">&</span></strong></tt><strong><span style="color: red">錛?/span></strong></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee boss(”Morris, Melinda”, 83000);</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* s = &boss;</span></tt></p> <p><strong><span style="color: red">榪欏疄闄呬笂騫朵笉鏄粈涔堝ソ涓繪剰銆備繚闄╃殑鍋氭硶榪樻槸搴旇鐩存帴璁╂寚閽堟寚鍚戜嬌鐢?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">new</span></strong></tt><strong><span style="color: red">鐢熸垚鐨勬柊瀵硅薄銆?/span></strong></p> <p><strong><span style="color: red">鍒扮洰鍓嶄負姝紝</span><span style="color: blue">C++ </span><span style="color: blue">鎸囬拡</span><span style="color: red">鐪嬭搗鏉ラ潪甯稿儚 </span><span style="color: blue">Java </span><span style="color: blue">鐨勫璞″紩鐢ㄥ彉閲?/span><span style="color: red">銆傜劧鑰岋紝榪欓噷鏈変竴涓緢閲嶈鐨勮娉曠殑涓嶅悓銆?/span></strong>鎴戜滑蹇呴』浣跨敤鏄熷彿鎿嶄綔絎?<tt><span style="font-size: 12pt; line-height: 150%">*</span></tt>鏉ヨ闂寚閽堟寚鍚戠殑瀵硅薄銆傚鏋?<tt><span style="font-size: 12pt; line-height: 150%">p</span></tt>鏄竴涓寚鍚?tt><span style="font-size: 12pt; line-height: 150%">Employee</span></tt>瀵硅薄鐨勬寚閽堬紝鍒?<tt><span style="font-size: 12pt; line-height: 150%">*p</span></tt>鎵嶄唬琛ㄤ簡榪欎釜瀵硅薄錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* p = . . .;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee boss = </span></tt><strong><span style="font-family: 瀹嬩綋">*p</span></strong><tt><span style="font-size: 12pt; line-height: 150%">;</span></tt></p> <p>褰撴垜浠渶瑕佹墽琛屽璞$殑鍑芥暟鎴栬闂璞$殑涓€涓暟鎹煙鏃訛紝涔熼渶瑕佷嬌鐢?<tt><span style="font-size: 12pt; line-height: 150%">*p</span></tt>錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">(*p).setSalary(91000);</span></tt></p> <p>*p澶栭潰鐨勬嫭鍙鋒槸蹇呴渶鐨勶紝鍥犱負 <tt><span style="font-size: 12pt; line-height: 150%">.</span></tt>鎿嶄綔絎︽瘮 * 鎿嶄綔絎︽湁鏇撮珮鐨勪紭鍏堢駭銆侰鐨勮璁¤€呰寰楄繖縐嶅啓娉曞緢闅劇湅錛屾墍浠ヤ粬浠彁渚涗簡鍙﹀涓€縐嶆浛浠g殑鍐欐硶錛屼嬌鐢?-> 鎿嶄綔絎︽潵瀹炵幇 <tt><span style="font-size: 12pt; line-height: 150%">*</span></tt>鍜?<tt><span style="font-size: 12pt; line-height: 150%">.</span></tt>鎿嶄綔絎︾殑緇勫悎鍔熻兘銆傝〃杈懼紡</p> <p><tt><span style="font-size: 12pt; line-height: 150%">p->setSalary(91000);</span></tt></p> <p>鍙互璋冪敤瀵硅薄*p鐨勫嚱鏁?<tt><span style="font-size: 12pt; line-height: 150%">setSalary</span></tt>銆備綘鍙互綆€鍗曠殑璁頒綇<tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">.</span></strong></tt><strong><span style="color: blue">鎿嶄綔絎︽槸鍦ㄥ璞′笂浣跨敤鐨勶紝-> 鎿嶄綔絎︽槸鍦ㄦ寚閽堜笂浣跨敤鐨勩€?/span></strong></p> <p><strong><span style="color: blue">濡傛灉浣犱笉鍒濆鍖栦竴涓寚閽堬紝鎴栬€呭鏋滀竴涓寚閽堜負絀哄€?NULL 鎴栨寚鍚戠殑瀵硅薄涓嶅啀瀛樺湪錛屽垯鍦ㄥ畠涓婇潰浣跨敤 </span></strong><tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">*</span></strong></tt><strong><span style="color: blue">鎴?</span></strong><tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">-></span></strong></tt><strong><span style="color: blue">鎿嶄綔絎﹀氨浼氬嚭閿欍€?涓嶅垢鐨勬槸 C++ runtime 緋葷粺騫朵笉媯€鏌ヨ繖涓嚭閿欍€傚鏋滀綘鑼冧簡榪欎釜閿欒錛屼綘鐨勭▼搴忓彲鑳戒細琛屼負鍙ゆ€垨姝繪満銆?/span></strong></p> <p><strong> </strong></p> <p>鑰屽湪Java涓紝榪欎簺閿欒鏄笉浼氬彂鐢熺殑銆傛墍鏈夌殑reference閮藉繀欏誨垵濮嬪寲錛屾墍鏈夌殑瀵硅薄鍙浠嶆湁reference鎸囧悜瀹冨氨涓嶄細琚粠鍐呭瓨涓竻闄わ紝鍥犳浣犱篃涓嶄細鏈変竴涓寚鍚戝凡琚垹闄ょ殑瀵硅薄鐨剅eference銆侸ava鐨剅untime 緋葷粺浼氭鏌eference鏄惁涓虹┖錛屽茍鍦ㄩ亣鍒扮┖鎸囬拡鏃舵姏鍑轟竴涓猲ull pointer鐨勪緥澶?exception)銆?/p> <p><strong><span style="color: blue">C++ </span><span style="color: blue">鍜?Java榪樻湁涓€涓樉钁楃殑涓嶅悓錛屽氨鏄?Java 鏈?em><span style="font-family: 瀹嬩綋">鍨冨溇鍥炴敹</span></em>鍔熻兘錛岃兘澶熻嚜鍔ㄥ洖鏀惰搴熷純鐨勫璞°€傝€屽湪C++涓紝闇€瑕佺▼搴忓憳鑷繁綆$悊鍐呭瓨鍒嗛厤鍥炴敹銆?/span></strong></p> <p>C++涓綋瀵硅薄鍙橀噺瓚呭嚭鑼冨洿鏃跺彲浠ヨ嚜鍔ㄨ鍥炴敹銆備絾鏄嬌鐢╪ew鐢熸垚鐨勫璞″繀欏葷敤delete鎿嶄綔絎︽墜鍔ㄥ垹闄わ紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* p = new Employee(”Hacker, Harry”, 38000);</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">. . .</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">delete p; /* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">涓嶅湪闇€瑕佽繖涓璞?*/</span></tt></p> <p>濡傛灉浣犲繕璁板垹闄や竴涓璞★紝閭d箞浣犵殑紼嬪簭鏈夊彲鑳芥渶緇堢敤鍏夋墍鏈夊唴瀛樸€傝繖灝辨槸鎴戜滑甯歌鐨勫唴瀛樻硠婕?(<em><span style="font-family: 瀹嬩綋">memory leak</em>)</span>銆傛洿閲嶈鐨勬槸錛屽鏋滀綘濡傛灉鍒犻櫎浜嗕竴涓璞★紝鐒跺悗鍙堢戶緇嬌鐢ㄥ畠錛屼綘鍙兘瑕嗙洊涓嶅睘浜庝綘鐨勬暟鎹€傚鏋滀綘鍒氬閥瑕嗙洊浜嗙敤浜庡鐞嗗唴瀛樺洖鏀剁殑鏁版嵁鍩燂紝閭d箞鍐呭瓨鍒嗛厤鏈哄埗灝卞彲鑳借繍杞け甯歌€岄€犳垚鏇翠弗閲嶇殑閿欒錛岃€屼笖寰堥毦璇婃柇鍜屼慨澶嶃€傚洜姝わ紝鍦–++涓渶濂藉敖閲忓皯鐢ㄦ寚閽堛€?/p> <h2>9. 緇ф壙</h2> <p>C++鍜孞ava涓戶鎵跨殑鍩烘湰璇硶鏄緢鐩鎬技鐨勩€傚湪C++涓紝浣跨敤 <tt><span style="font-size: 12pt; line-height: 150%">: public</span></tt>浠f浛Java涓殑<tt><span style="font-size: 12pt; line-height: 150%">extends</span></tt>鏉ヨ〃紺虹戶鎵垮叧緋?銆?(C++ 涔熸敮鎸佺鏈夌戶鎵跨殑姒傚康錛屼絾鏄笉澶湁鐢ㄣ€?</p> <p>榛樿鎯呭喌涓嬶紝C++涓殑鍑芥暟涓嶆槸鍔ㄦ€佺粦瀹氱殑銆傚鏋滀綘闇€瑕佹煇涓嚱鏁板疄鐜板姩鎬佺粦瀹氾紝闇€瑕佷嬌鐢?tt><span style="font-size: 12pt; line-height: 150%">virtual</span></tt>澹版槑瀹冧負铏氬嚱鏁幫紝渚嬪錛?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">class Manager : public Employee</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ </span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">public:</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">Manager(string name, double salary, string dept);</span></tt></p> <p><strong><span style="font-family: 瀹嬩綋">virtual</span></strong><tt><span style="font-size: 12pt; line-height: 150%"> void print() const;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">private:</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">string department;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">};</span></tt></p> <p>鍚孞ava涓€鏍鳳紝鏋勯€犲嚱鏁頒腑璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁版湁鐗規畩鐨勮娉曘€?Java浣跨敤鍏抽敭瀛?<tt><span style="font-size: 12pt; line-height: 150%">super</span></tt>銆侰++涓繀欏誨湪瀛愮被鐨勬瀯閫犲嚱鏁頒綋澶栬皟鐢ㄧ埗綾葷殑鏋勯€犲嚱鏁般€備笅闈㈡槸涓€涓緥瀛愶細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">Manager::Manager(string name, double salary, string dept)</span></tt></p> <p><strong><span style="font-family: 瀹嬩綋">: Employee(name, salary) </span></strong><tt><span style="font-size: 12pt; line-height: 150%">/* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁?*/</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ department = dept;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p>Java 涓湪瀛愮被鍑芥暟涓皟鐢ㄧ埗綾葷殑鍑芥暟鏃朵篃浣跨敤鍏抽敭瀛?<tt><span style="font-size: 12pt; line-height: 150%">super</span></tt>銆傝€屽湪C++涓槸浣跨敤鐖剁被鐨勫悕縐板姞涓婃搷浣滅 <tt><span style="font-size: 12pt; line-height: 150%">::</span></tt>琛ㄧず錛屼緥濡傦細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">void Manager::print() const</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">{ </span></tt><strong><span style="font-family: 瀹嬩綋">Employee::print(); </span></strong><tt><span style="font-size: 12pt; line-height: 150%">/* </span></tt><tt><span style="font-size: 12pt; line-height: 150%">璋冪敤鐖剁被鐨勫嚱鏁?*/</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">cout << department << “"n”;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">}</span></tt></p> <p>涓€涓?C++ 瀵硅薄鍙橀噺鍙兘瀛樺偍鐗瑰畾綾誨瀷鐨勫璞″€箋€傝鎯沖湪C++涓疄鐜板鎬?polymorphism)錛屽繀欏諱嬌鐢ㄦ寚閽堛€備竴涓?<tt><span style="font-size: 12pt; line-height: 150%">T*</span></tt>鎸囬拡鍙互鎸囧悜綾誨瀷涓?<tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>鎴?<tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>鐨勪換鎰忓瓙綾葷殑瀵硅薄錛屼緥濡傦細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* e = new Manager(”Morris, Melinda”, 83000, “Finance”);</span></tt></p> <p>浣犲彲浠ュ皢鐖剁被鍜屼笉鍚屽瓙綾葷殑瀵硅薄娣峰悎鏀墮泦鍒頒竴涓厓绱犲潎涓烘寚閽堢殑鍚戦噺涓紝鐒跺悗璋冪敤鍔ㄦ€佺粦瀹氱殑鍑芥暟錛屽涓嬫墍紺猴細</p> <p><tt><span style="font-size: 12pt; line-height: 150%">vector<Employee*> staff;</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">. . .</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">for (i = 0; i < staff.size(); i++)</span></tt></p> <p><tt><span style="font-size: 12pt; line-height: 150%">staff[i]->print();</span></tt></p> <img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/270049.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-11 14:58 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270049.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鐢變竴孌礐++渚嬪瓙鎯寵搗鐨勶紝涓昏鍏充簬C++涓殑涓婁笅杞瀷闂http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269940.htmlFrank_FangFrank_FangSun, 10 May 2009 13:13:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269940.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/269940.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269940.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269940.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/269940.html void funOne(Sub &s);
鍦ㄤ笉鍚戜笅杞瀷鐨勬儏鍐典笅鏄惁鑳藉鐩存帴榪欐牱璋冪敤funOne(*p)
鍗犱綅
鎸囬拡錛屽紩鐢紝瀵硅薄

]]>
C++綾繪垚鍛樺彉閲忓垵濮嬪寲鍙婁笌Java涓殑鍖哄埆http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269936.htmlFrank_FangFrank_FangSun, 10 May 2009 13:07:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269936.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/269936.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269936.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269936.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/269936.html Java涓殑鎴愬憳鍙橀噺鐨勫垵濮嬪寲欏哄簭

C++涓殑鎴愬憳鍙橀噺鐨勫垵濮嬪寲欏哄簭錛屼富瑕佹槸鍐呯疆綾誨瀷鐨勫彉閲忔槸鍚︾粰鍏惰祴鍒濆€間簡
C++涓殑鍐呯疆綾誨瀷primitive鍦ㄥ叏灞€鍖哄煙錛堟敞鎰忎粈涔堟槸鍏ㄥ眬鍖哄煙)鏄細琚郴緇熻嚜鍔ㄥ垵濮嬪寲鐨?涓?,鍦ㄥ眬閮ㄥ尯鍩熸槸涓嶄細琚嚜鍔ㄥ垵濮嬪寲鐨?br /> C++涓殑綾葷被鍨嬬殑鍙橀噺鏄細琚嚜鍔ㄥ垵濮嬪寲鐨勶紝浣嗚姹傚叾鏈夐粯璁ょ殑鏋勯€犲嚱鏁?br /> 緇ф壙鏃禖++鐨勬垚鍛樺彉閲忎笌Java涓被浼?

]]>
C++澶氶噸緇ф壙闂鍙婅櫄緇ф壙virtual鐨勯棶棰?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269935.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Sun, 10 May 2009 12:57:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269935.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/269935.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269935.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269935.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/269935.html</trackback:ping><description><![CDATA[鍗犱綅 <br /> 澶氶噸緇ф壙闂,浜屼箟鎬ч棶棰?br /> sizeof(A)鐨勯棶棰?鏅€氱戶鎵垮拰铏氱戶鎵跨殑宸埆 <img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/269935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-10 20:57 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269935.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>涓婁竴綃嘋++铏氬嚱鏁頒緥瀛?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269549.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Fri, 08 May 2009 02:44:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269549.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/269549.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269549.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269549.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/269549.html</trackback:ping><description><![CDATA[    鍙湁娉ㄥ唽鐢ㄦ埛鐧誨綍鍚庢墠鑳介槄璇昏鏂囥€?a href='http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269549.html'>闃呰鍏ㄦ枃</a><img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/269549.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-08 10:44 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269549.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>*铏氬嚱鏁拌〃涓庤櫄琛ㄦ寚閽?杞?http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269540.htmlFrank_FangFrank_FangFri, 08 May 2009 02:15:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269540.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/269540.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269540.html#Feedback2http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269540.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/269540.html闃呰鍏ㄦ枃

]]>
Java涓笌C++鐨勫張涓€涓嶅悓鐐?瀛愮被,鐖剁被鏂規硶鐨勯噸杞斤紝瑕嗙洊錛岄殣钘?/title><link>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269456.html</link><dc:creator>Frank_Fang</dc:creator><author>Frank_Fang</author><pubDate>Thu, 07 May 2009 09:37:00 GMT</pubDate><guid>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269456.html</guid><wfw:comment>http://www.aygfsteel.com/fhtdy2004/comments/269456.html</wfw:comment><comments>http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269456.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269456.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/fhtdy2004/services/trackbacks/269456.html</trackback:ping><description><![CDATA[    鍙湁娉ㄥ唽鐢ㄦ埛鐧誨綍鍚庢墠鑳介槄璇昏鏂囥€?a href='http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269456.html'>闃呰鍏ㄦ枃</a><img src ="http://www.aygfsteel.com/fhtdy2004/aggbug/269456.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-07 17:37 <a href="http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269456.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>C++ 鍙嬪厓綾誨拰鍙嬪厓鍑芥暟http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269418.htmlFrank_FangFrank_FangThu, 07 May 2009 07:10:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269418.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/269418.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269418.html#Feedback1http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269418.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/269418.html闃呰鍏ㄦ枃

]]>
杞?RTTI鐗規€у皬絀?dynamic_cast杞崲鎿嶄綔絎﹀拰typeid鎿嶄綔絎?http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269414.htmlFrank_FangFrank_FangThu, 07 May 2009 06:20:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269414.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/269414.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269414.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269414.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/269414.html闃呰鍏ㄦ枃

]]>
浣跨敤c++搴旀敞鎰忕殑鍦版柟(涓€)http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269412.htmlFrank_FangFrank_FangThu, 07 May 2009 06:08:00 GMThttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269412.htmlhttp://www.aygfsteel.com/fhtdy2004/comments/269412.htmlhttp://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269412.html#Feedback0http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269412.htmlhttp://www.aygfsteel.com/fhtdy2004/services/trackbacks/269412.html闃呰鍏ㄦ枃

]]>
主站蜘蛛池模板: 西安市| 珲春市| 武山县| 泗洪县| 孟州市| 民和| 辽宁省| 冷水江市| 金门县| 汉中市| 长顺县| 包头市| 遂平县| 西贡区| 建平县| 浮梁县| 达孜县| 阿鲁科尔沁旗| 巴青县| 洛扎县| 洛川县| 阳西县| 米林县| 芦山县| 仙游县| 南部县| 陆河县| 潍坊市| 准格尔旗| 青冈县| 故城县| 玛纳斯县| 康马县| 余姚市| 晋宁县| 长治市| 福安市| 昭觉县| 洛扎县| 柞水县| 军事|