*杞?C++ 瀵硅薄鐨勫唴瀛樺竷灞€(涓? http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270922.htmlFrank_Fang Frank_Fang Fri, 15 May 2009 12:14:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270922.html http://www.aygfsteel.com/fhtdy2004/comments/270922.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270922.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270922.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/270922.html 闃呰鍏ㄦ枃 ]]> *杞細C++瀵硅薄鐨勫唴瀛樺竷灞€(涓? http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270916.htmlFrank_Fang Frank_Fang Fri, 15 May 2009 11:16:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270916.html http://www.aygfsteel.com/fhtdy2004/comments/270916.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/15/270916.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270916.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/270916.html 闃呰鍏ㄦ枃 ]]> C++鍑芥暟榪斿洖鑷敱鍐呭瓨鍖洪棿鐨勫璞$殑寮曠敤 http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270125.htmlFrank_Fang Frank_Fang Mon, 11 May 2009 15:01:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270125.html http://www.aygfsteel.com/fhtdy2004/comments/270125.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270125.html#Feedback 2 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270125.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/270125.html Effective 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 * s = 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_Fang Frank_Fang Mon, 11 May 2009 13:16:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270110.html http://www.aygfsteel.com/fhtdy2004/comments/270110.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/11/270110.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/270110.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/270110.html JAVA涓瓙綾誨璞$殑瀹炰緥鍖栬繃紼?/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">姝e4妤肩殑鎵€璇磣~~ <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_Fang Frank_Fang Sun, 10 May 2009 13:13:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269940.html http://www.aygfsteel.com/fhtdy2004/comments/269940.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269940.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269940.html http://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_Fang Frank_Fang Sun, 10 May 2009 13:07:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269936.html http://www.aygfsteel.com/fhtdy2004/comments/269936.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/10/269936.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269936.html http://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_Fang Frank_Fang Fri, 08 May 2009 02:15:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269540.html http://www.aygfsteel.com/fhtdy2004/comments/269540.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/08/269540.html#Feedback 2 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269540.html http://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_Fang Frank_Fang Thu, 07 May 2009 07:10:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269418.html http://www.aygfsteel.com/fhtdy2004/comments/269418.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269418.html#Feedback 1 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269418.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/269418.html 闃呰鍏ㄦ枃 ]]> 杞?RTTI鐗規€у皬絀?dynamic_cast杞崲鎿嶄綔絎﹀拰typeid鎿嶄綔絎? http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269414.htmlFrank_Fang Frank_Fang Thu, 07 May 2009 06:20:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269414.html http://www.aygfsteel.com/fhtdy2004/comments/269414.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269414.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269414.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/269414.html 闃呰鍏ㄦ枃 ]]> 浣跨敤c++搴旀敞鎰忕殑鍦版柟(涓€) http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269412.htmlFrank_Fang Frank_Fang Thu, 07 May 2009 06:08:00 GMT http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269412.html http://www.aygfsteel.com/fhtdy2004/comments/269412.html http://www.aygfsteel.com/fhtdy2004/archive/2009/05/07/269412.html#Feedback 0 http://www.aygfsteel.com/fhtdy2004/comments/commentRss/269412.html http://www.aygfsteel.com/fhtdy2004/services/trackbacks/269412.html 闃呰鍏ㄦ枃 ]]>