??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产精品综合久久久,亚洲视频免费观看,亚洲乱码在线视频http://www.tkk7.com/fhtdy2004/category/39265.htmll验薄Q耐心(j)U篏Q记性不好,记诸文字zh-cnSat, 31 Oct 2009 19:26:35 GMTSat, 31 Oct 2009 19:26:35 GMT60char str[]与char *str一个小tiphttp://www.tkk7.com/fhtdy2004/archive/2009/08/05/289968.htmlFrank_FangFrank_FangWed, 05 Aug 2009 08:57:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/08/05/289968.htmlhttp://www.tkk7.com/fhtdy2004/comments/289968.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/08/05/289968.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/289968.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/289968.html     const char str[] = "abc";
    char * s = const_cast<char *>(str);
    s[0] = 'A';
 
    cout<<s<<endl;    //输出Abc
但是q样׃行了(jin)Q?
   const char *str = "abc";
    char * s = const_cast<char *>(str);
    s[0] = 'A';
 
    cout<<s<<endl;    //~译能通过Q但是执行就报错?br />
: const char str[] = "abc";        abc存储在堆栈中Q?/font>
: const char *str = "abc";         abc存储在静(rn)态存储区Q?/font>
准确的说Q上面两?#8220;abc"都是存储在静(rn)态存储区Q即帔R区。常量区是可M可写的。所以Q何试囑֯帔R行写的操作都是非法的Q当然了(jin)Q这也不是一定不可写的,你可以采取某U渠道改变常量区的内存属性,比如改变pe相关节的属性就可以对常量区q行dQ当然了(jin)Q这个目前可以忽略。。?
那么Z么str[] = "abc";        可以写呢Q答案就在str[] = "abc";?x)有一个额外的拯q程Q即把常量区?"abc"拯到栈内存去,所以就可以写了(jin)?


Frank_Fang 2009-08-05 16:57 发表评论
]]>
C++引用结Q{ Q?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/06/07/280417.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/280417.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/06/07/280417.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/280417.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/fhtdy2004/services/trackbacks/280417.html</trackback:ping><description><![CDATA[     摘要: 介绍C++引用的基本概念,通过详细的应用分析与说明Q对引用q行全面、透彻地阐q?  <a href='http://www.tkk7.com/fhtdy2004/archive/2009/06/07/280417.html'>阅读全文</a><img src ="http://www.tkk7.com/fhtdy2004/aggbug/280417.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-06-07 12:23 <a href="http://www.tkk7.com/fhtdy2004/archive/2009/06/07/280417.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++中将构造函数或析构函数定义为privatehttp://www.tkk7.com/fhtdy2004/archive/2009/05/30/278971.htmlFrank_FangFrank_FangFri, 29 May 2009 16:22:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/30/278971.htmlhttp://www.tkk7.com/fhtdy2004/comments/278971.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/30/278971.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/278971.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/278971.html阅读全文

Frank_Fang 2009-05-30 00:22 发表评论
]]>
C++的static关键?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/30/278968.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/278968.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/05/30/278968.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/278968.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/fhtdy2004/services/trackbacks/278968.html</trackback:ping><description><![CDATA[<p>  C++的static有两U用法:(x)面向q程E序设计中的static和面向对象程序设计中的static。前者应用于普通变量和函数Q不涉及(qing)c;后者主要说明static在类中的作用?/p> <p><strong>一、面向过E设计中的static</strong></p> <p>1、静(rn)态全局变量</p> <p>在全局变量前,加上关键字staticQ该变量p定义成ؓ(f)一个静(rn)态全局变量。我们先举一个静(rn)态全局变量的例子,如下Q?</p> <pre>//Example 1 #include <iostream.h> void fn(); static int n; //定义?rn)态全局变量 void main() { n=20; cout<<n<<endl; fn(); } void fn() { n++; cout<<n<<endl; } </pre> ?rn)态全局变量有以下特点:(x) <ul> <li>该变量在全局数据区分配内存; <li>未经初始化的?rn)态全局变量?x)被E序自动初始化ؓ(f)0Q自动变量的值是随机的,除非它被昑ּ初始化)(j)Q? <li>?rn)态全局变量在声明它的整个文仉是可见的Q而在文g之外是不可见的;  </li> </ul> ?rn)态变量都在全局数据区分配内存,包括后面要提到的静(rn)态局部变量。对于一个完整的E序Q在内存?sh)的分布情况如下图?x)<br />   <table id="table1" width="10%" border="1"> <tbody> <tr> <td>代码?/td> </tr> <tr> <td>全局数据?/td> </tr> <tr> <td>堆区</td> </tr> <tr> <td>栈区</td> </tr> </tbody> </table> <p>  一般程序的由new产生的动态数据存攑֜堆区Q函数内部的自动变量存放在栈区。自动变量一般会(x)随着函数的退?gu)释攄_(d)?rn)态数据(即是函数内部的?rn)态局部变量)(j)也存攑֜全局数据区。全局数据区的数据q不?x)因为函数的退?gu)释攄间。细?j)的读者可能会(x)发现QExample 1中的代码中将 </p> <pre> static int n; //定义?rn)态全局变量</pre> 改ؓ(f) <pre> int n; //定义全局变量</pre> E序照样正常q行?br /> 的确Q定义全局变量可以实现变量在文g中的׃nQ但定义?rn)态全局变量q有以下好处Q? <ul> <li>?rn)态全局变量不能被其它文件所用; <li>其它文g中可以定义相同名字的变量Q不?x)发生冲H; </li> </ul> <p>(zhn)可以将上述CZ代码改ؓ(f)如下Q?br /> </p> <pre>//Example 2 //File1 #include <iostream.h> void fn(); static int n; //定义?rn)态全局变量 void main() { n=20; cout<<n<<endl; fn(); } //File2 #include <iostream.h> extern int n; void fn() { n++; cout<<n<<endl; } </pre> ~译q运行Example 2Q?zhn)׃?x)发现上述代码可以分别通过~译Q但q行时出现错误。试着? <pre>static int n; //定义?rn)态全局变量 </pre> 改ؓ(f) <pre>int n; //定义全局变量 </pre> 再次~译q行E序Q细?j)体会(x)全局变量和静(rn)态全局变量的区别? <p>2、静(rn)态局部变?/p> <p>在局部变量前Q加上关键字staticQ该变量p定义成ؓ(f)一个静(rn)态局部变量?</p> <p>我们先D一个静(rn)态局部变量的例子Q如下:(x) </p> <pre>//Example 3 #include <iostream.h> void fn(); void main() { fn(); fn(); fn(); } void fn() { static n=10; cout<<n<<endl; n++; } </pre>   通常Q在函数体内定义?jin)一个变量,每当E序q行到该语句旉?x)给该局部变量分配栈内存。但随着E序退出函CQ系l就?x)收回栈内存Q局部变量也相应失效?br />   但有时候我们需要在两次调用之间对变量的D行保存。通常的想法是定义一个全局变量来实现。但q样一来,变量已经不再属于函数本n?jin),不再仅受函数的控Ӟl程序的l护带来不便?br />   ?rn)态局部变量正好可以解册个问题。静(rn)态局部变量保存在全局数据区,而不是保存在栈中Q每ơ的g持到下一ơ调用,直到下次赋新倹{? <p>?rn)态局部变量有以下特点Q?/p> <ul> <li>该变量在全局数据区分配内存; <li>?rn)态局部变量在E序执行到该对象的声明处时被首次初始化,即以后的函数调用不再q行初始化; <li>?rn)态局部变量一般在声明处初始化Q如果没有显式初始化Q会(x)被程序自动初始化?Q? <li>它始l驻留在全局数据区,直到E序q行l束。但其作用域为局部作用域Q当定义它的函数或语句块l束Ӟ其作用域随之l束Q?</li> </ul> <p>3、静(rn)态函?/p> <p>  在函数的q回cd前加上static关键?函数卌定义为静(rn)态函数。静(rn)态函C普通函C同,它只能在声明它的文g当中可见Q不能被其它文g使用?/p> <p>?rn)态函数的例子Q?</p> <pre>//Example 4 #include <iostream.h> static void fn();//声明?rn)态函? void main() { fn(); } void fn()//定义?rn)态函? { int n=10; cout<<n<<endl; } </pre> 定义?rn)态函数的好处Q? <ul> <li>?rn)态函C能被其它文g所用; <li>其它文g中可以定义相同名字的函数Q不?x)发生冲H; </li> </ul> <p><strong>二、面向对象的static关键字(cM的static关键字)(j)</strong></p> <p>1、静(rn)态数据成?/p> <p>在类内数据成员的声明前加上关键字staticQ该数据成员是cd的静(rn)态数据成员。先举一个静(rn)态数据成员的例子?</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;//声明?rn)态数据成? }; int Myclass::Sum=0;//定义q初始化?rn)态数据成? 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> 可以看出Q静(rn)态数据成员有以下特点Q? <ul> <li>对于非静(rn)态数据成员,每个cd象都有自q拯。而静(rn)态数据成员被当作是类的成员。无个类的对象被定义?jin)多个Q静(rn)态数据成员在E序中也只有一份拷贝,pcd的所有对象共享访问。也是_(d)?rn)态数据成员是该类的所有对象所共有的。对该类的多个对象来_(d)?rn)态数据成员只分配一ơ内存,供所有对象共用。所以,?rn)态数据成员的值对每个对象都是一L(fng)Q它的值可以更斎ͼ <li>?rn)态数据成员存储在全局数据区。静(rn)态数据成员定义时要分配空_(d)所以不能在cd明中定义。在Example 5中,语句int Myclass::Sum=0;是定义静(rn)态数据成员; <li>?rn)态数据成员和普通数据成员(sh)样遵从public,protected,private讉K规则Q? <li>因ؓ(f)?rn)态数据成员在全局数据区分配内存,属于本类的所有对象共享,所以,它不属于特定的类对象Q在没有产生cd象时其作用域可见,卛_没有产生cȝ实例Ӟ我们可以操作它Q? <li>?rn)态数据成员初始化与一般数据成员初始化不同。静(rn)态数据成员初始化的格式ؓ(f)Q?br /> Q数据类型>Q类名>::Q静(rn)态数据成员名Q?Q| <li>cȝ?rn)态数据成员有两种讉K形式Q?br /> Q类对象名>.Q静(rn)态数据成员名Q??Q类cd名>::Q静(rn)态数据成员名Q?br /> 如果?rn)态数据成员的讉K权限允许的话Q即public的成员)(j)Q可在程序中Q按上述格式来引用静(rn)态数据成?Q? <li>?rn)态数据成员(sh)要用在各个对象都有相同的某项属性的时候。比如对于一个存?gu)ƄQ每个实例的利息都是相同的。所以,应该把利息设为存?gu)Ƅ的?rn)态数据成员。这有两个好处,W一Q不定义多个存款cd象,利息数据成员都共享分配在全局数据区的内存Q所以节省存储空间。第二,一旦利息需要改变时Q只要改变(sh)ơ,则所有存?gu)Ƅ对象的利息全改变q来?jin)? <li>同全局变量相比Q用静(rn)态数据成员有两个优势Q?</li> </ul> <ol> <li>?rn)态数据成员没有进入程序的全局名字I间Q因此不存在与程序中其它全局名字冲突的可能性; <li>可以实现信息隐藏。静(rn)态数据成员可以是private成员Q而全局变量不能Q?br /> </li> </ol> 2、静(rn)态成员函? <p>  与静(rn)态数据成员(sh)P我们也可以创Z个静(rn)态成员函敎ͼ它ؓ(f)cȝ全部服务而不是ؓ(f)某一个类的具体对象服务。静(rn)态成员函C?rn)态数据成员(sh)P都是cȝ内部实现Q属于类定义的一部分。普通的成员函数一般都隐含?jin)一个this指针Qthis指针指向cȝ对象本nQ因为普通成员函数L具体的属于某个类的具体对象的。通常情况下,this是缺省的。如函数fn()实际上是this->fn()。但是与普通函数相比,?rn)态成员函数由于不是与M的对象相联系Q因此它不具有this指针。从q个意义上讲Q它无法讉K属于cd象的非静(rn)态数据成员,也无法访问非?rn)态成员函敎ͼ它只能调用其余的?rn)态成员函数。下面D个静(rn)态成员函数的例子?</p> <pre>//Example 6 #include <iostream.h> class Myclass { public: Myclass(int a,int b,int c); static void GetSum();/声明?rn)态成员函? private: int a,b,c; static int Sum;//声明?rn)态数据成? }; int Myclass::Sum=0;//定义q初始化?rn)态数据成? Myclass::Myclass(int a,int b,int c) { this->a=a; this->b=b; this->c=c; Sum+=a+b+c; //非静(rn)态成员函数可以访问静(rn)态数据成? } void Myclass::GetSum() //?rn)态成员函数的实现 { // cout<<a<<endl; //错误代码Qa是非?rn)态数据成? 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> 关于?rn)态成员函敎ͼ可以ȝZ下几点:(x) <p> </p> <ul> <li>出现在类体外的函数定义不能指定关键字staticQ? <li>?rn)态成员(sh)间可以相互访问,包括?rn)态成员函数访问静(rn)态数据成员和讉K?rn)态成员函敎ͼ <li>非静(rn)态成员函数可以Q意地讉K?rn)态成员函数和?rn)态数据成员; <li>?rn)态成员函C能访问非?rn)态成员函数和非静(rn)态数据成员; <li>׃没有this指针的额外开销Q因此静(rn)态成员函Ccȝ全局函数相比速度上会(x)有少许的增长Q? <li>调用?rn)态成员函敎ͼ可以用成员访问操作符(.)?->)Z个类的对象或指向cd象的指针调用?rn)态成员函敎ͼ也可以直接用如下格式:(x)<br /> Q类名>::Q静(rn)态成员函数名Q(Q参数表Q)(j)<br /> 调用cȝ?rn)态成员函数?</li> </ul> <img src ="http://www.tkk7.com/fhtdy2004/aggbug/278968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-30 00:06 <a href="http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/29/278958.htmlFrank_FangFrank_FangFri, 29 May 2009 15:20:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/29/278958.htmlhttp://www.tkk7.com/fhtdy2004/comments/278958.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/29/278958.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/278958.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/278958.htmlC语言中讲讲static变量和static函数有什么作?/strong>
static关键字有两种意?你看上下文来判断

1,表示变量是静(rn)态存储变?
表示变量存放在静(rn)态存储区.
2,表示该变量是内部q接
(q种情况是指该变量不在Q何{}之内,p全局变量那样,q时候加上static)
,也就是说在其它的.cpp文g?该变量是不可见的(你不能用).

当static加在函数前面的时?
表示该函数是内部q接,之在本文件中有效,别的文g中不能应用该函数.
不加static的函数默认ؓ(f)是全局?
也就是说在其他的.cpp中只要申明一下这个函?可以用它.

1、static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
    {:(x)全局变量(外部变量)的说明之前再冠以static 构成了(jin)?rn)态的全局变量。全局变量本n是?rn)态存储方式, ?rn)态全局变量当然也是?rn)态存储方式?q两者在存储方式上ƈ无不同。这两者的区别虽在于非?rn)态全局变量的作用域是整个源E序Q当一个源E序由多个源文gl成Ӟ非静(rn)态的全局变量在各个源文g中都是有效的?而静(rn)态全局变量则限制了(jin)其作用域Q?卛_在定义该变量的源文g内有效, 在同一源程序的其它源文件中不能使用它。由于静(rn)态全局变量的作用域局限于一个源文g内,只能源文件内的函数公用, 因此可以避免在其它源文g中引起错误?br />     从以上分析可以看出, 把局部变量改变(sh)ؓ(f)?rn)态变量后是改变(sh)(jin)它的存储方式x变(sh)(jin)它的生存期。把全局变量改变?sh)?rn)态变量后是改变(sh)(jin)它的作用域, 限制?jin)它的用范围?br />     static函数与普通函C用域不同。static函数仅在本文件中使用。只在当前源文g中用的函数应该说明为内部函?static)Q内部函数应该在当前源文件中说明和定义。对于可在当前源文g以外使用的函敎ͼ应该在一个头文g中说明,要用这些函数的源文件要包含q个头文?br />     static全局变量与普通的全局变量有什么区别:(x)static全局变量只初使化一ơ,防止在其他文件单元中被引?
    static局部变量和普通局部变量有什么区别:(x)static局部变量只被初始化一ơ,下一ơ依据上一ơ结果|
    static函数与普通函数有什么区别:(x)static函数在内存(sh)只有一份,普通函数在每个被调用中l持一份拷?/p>

2、如何引用一个已l定义过的全局变量Q?/strong>
     {:(x)extern
     可以用引用头文g的方式,也可以用extern关键字,如果用引用头文g方式来引用某个在头文件中声明的全局变理Q假定你那个变写错?jin),那么在编译期??x)报错,如果你用extern方式引用Ӟ假定你犯?jin)同L(fng)错误Q那么在~译期间不会(x)报错Q而在q接期间报错?/p>

3、全局变量可不可以定义在可被多?C文g包含的头文g中?Z么?
    {:(x)可以Q在不同的C文g中以static形式来声明同名全局变量?br />     可以在不同的C文g中声明同名的全局变量Q前提是其中只能有一个C文g中对此变量赋初|此时q接不会(x)出错?/p>

 



Frank_Fang 2009-05-29 23:20 发表评论
]]>
*?C++ 对象的内存布局(?http://www.tkk7.com/fhtdy2004/archive/2009/05/15/270922.htmlFrank_FangFrank_FangFri, 15 May 2009 12:14:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/15/270922.htmlhttp://www.tkk7.com/fhtdy2004/comments/270922.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/15/270922.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/270922.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/270922.html阅读全文

Frank_Fang 2009-05-15 20:14 发表评论
]]>
*转:(x)C++对象的内存布局(?http://www.tkk7.com/fhtdy2004/archive/2009/05/15/270916.htmlFrank_FangFrank_FangFri, 15 May 2009 11:16:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/15/270916.htmlhttp://www.tkk7.com/fhtdy2004/comments/270916.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/15/270916.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/270916.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/270916.html阅读全文

Frank_Fang 2009-05-15 19:16 发表评论
]]>
C++函数q回自由内存区间的对象的引用http://www.tkk7.com/fhtdy2004/archive/2009/05/11/270125.htmlFrank_FangFrank_FangMon, 11 May 2009 15:01:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/11/270125.htmlhttp://www.tkk7.com/fhtdy2004/comments/270125.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/11/270125.html#Feedback2http://www.tkk7.com/fhtdy2004/comments/commentRss/270125.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/270125.htmlEffective C++
条款31: 千万不要q回局部对象的引用Q也不要q回函数内部用new初始化的指针的引?/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”: 只有?rn)态常量整型数据成员才可以在类中初始化
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;//q个值区别于java不是赋初gؓ(f)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;//不能对引用用delete
55     delete pCat;
56     //delete好像没有释放内存Q怎么获取的还是原来的?br /> 57     //可能在这个内存区域存攄q是原来?先new string后再调用也没变,与编译器有关q是什??
58     for(int i =0;i<10;i++)
59     {
60         //想通过创徏string对象来填充之前的内存区间Q好像没?/span>
61         string *= new string("abcdefghijklmn");
62     }
63 
64     //q时问题来了(jin),rCat.getAge()?23?/span>
65     SimpleCat *pSecond = new SimpleCat(123,444);
66     
67     cout<<"delete pCat后再使用rCat引用?x)发生什么问???"<<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();//q个?x)发生内存泄漏,在函C甌的内存(sh)(x)得不到释?br /> 73                                      //myCat2是通过默认的拷贝函数来q行初始化的
74     cout<<"myCat2的地址?nbsp;"<<&myCat2<<endl;
75     return 0;
76 }
77 
78 
79 //q个函数在返回时Q是否会(x)创徏一个(f)时引用变???
80 //TheFunctionTwo(SimpleCat &simpleCat)q个函数在传递参数时是否?x)创Z个(f)时的引用变量
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()的返回D赋值给对SimpleCatcȝ一个引?然后使用q个引用来获得对象的成员变量age的?br /> Z(jin)证明函数TheFunction()在自由存储区中创建的对象被赋l了(jin)函数main()声明的引用,对rCat采取取址q算Q很明显Q它昄?jin)其引用的对象的地址Qƈ且和自由存储Z的对象的地址相匹配?br /> q一切看上去都很正确Q但是如何释放被占用的内存,对于引用是不能用deleteq算W的Q一个笔记聪明的Ҏ(gu)是声明另一个指针,q用由rCat得到的地址对它q行初始化。这样做实可以释放内存Q避免了(jin)内存的泄漏,但是存在一个小问题Q在delete pCat后,rCat引用又代表什么呢Q?引用必须是一个实际对象的别名Q如果引用一个空对象的话Q现在就是这U情况)(j)那么E序是无效的。?/p>

注意Q不能过分去一个用对I对象的引用的程序,也行能通过~译Q但是即使程序通过?jin)编译,它仍然是病态的Q而且执行l果也是不可预料?/p>

关于q个问题Q存在这3U解x?br /> W?U是在第49行声明一个SimpleCatcd象,然后从函数TheFunction()中采取g递的方式q回它的局部对象,q时不能在自由存储区来分配对象空_(d)不能使用newQ?br /> W?U是在函数TheFunction()中声明一个位于自由存储区中的SimpleCatcȝ对象Q但是让函数q回一个指向内存区域的指针Q然后完成对对象的操作,调用函数可以删除q个指针
W?U可行的Ҏ(gu)Q也是正的Q是在调用函C声明对象Q然后通过引用的方式把它传递给函数TheFunction()


也意呛_main函数中不能这样用了(jin)



Frank_Fang 2009-05-11 23:01 发表评论
]]>
java对象的实例化q程,成员变量的初始化序http://www.tkk7.com/fhtdy2004/archive/2009/05/11/270110.htmlFrank_FangFrank_FangMon, 11 May 2009 13:16:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/11/270110.htmlhttp://www.tkk7.com/fhtdy2004/comments/270110.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/11/270110.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/270110.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/270110.htmlJAVA中子cd象的实例化过E?/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
 }
}

其语句的执行序是怎样的呢Q?/span>
java
中,在?/span>new操作W创Z个类的实例对象的时候,开始分配空间ƈ成员变量初始化?/span>默认?/span>数?/span>Q注意这里ƈ不是指将变量初始化ؓ(f)在变量定义处的初始|?strong>是给整Ş赋?/span>
0Q给字符串赋?/span>null q一点于C++不同Q(student.name = null , student.age = 0 Q?/span>
然后在进入类的构造函数?/span>

在构造函数里面,首先要检查是否有this或?/span>super调用Q?/span>this调用是完成本cLw的构造函C间的调用Q?/span>super调用是完成对父类的调用?strong>二者只能出C个,q且只能作ؓ(f)构造函数的W一句出现?/span>在调?/span>this?/span>super的时候实现程序的跌{Q{而执行被调用?/span>this构造函数或?/span>super构造函数?/span>
?/span>this?/span>super执行完毕Q?strong>E序转而执行在cd义的时候进行的变量初始化工?/span>?/span>
q个执行完毕Q才是构造函C剩下的代码的执行?/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—even 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();
} ///:~


l承情况下的初始?br /> ?jin)解一下包括承在内的初始化的q程是非常有益的,q样p有个?br /> 体的?jin)解。看看下面这D代码:(x)
//: 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 q行Beetle 的时候,W一件事是讉K?/span>Beetel.main( )(q是一?/span>static ?span style="color: blue">?/span>)Q于是装载器(loader)׃(x)?/span>

你寻扄~译?/span>Beetle cȝ代码(也就?/span>Beetle.class 文g)。在装蝲的过E中Q装载器注意到它有一个基c?/span>(也就?/span>extends 所要表C的意?/span>)Q于是它再装载基cR不你创不创徏基类对象Q这个过EM(x)发生?/span>(试试看,把创建对象的那句注释掉,看看?x)有什么结果?/span>)如果基类q有基类Q那么这W二个基cM?x)被装蝲Q以此类?span style="color: blue">?strong>下一步,它会(x)执行“根基c?/strong>(root bas e class)(q里是Insect)?/span>static 初始化,然后是下一个派生类?/span>static 初始化,以此cL。这个顺序非帔R要,因ؓ(f)zcȝ“?rn)态初始化(卛_面讲?/span>static 初始?/span>)”有可能要依赖基类成员的正初始化?/span>

现在所有必要的c都已经装蝲l束Q可以创建对象了(jin)?/span>首先Q对象里的所有的primitive 都会(x)被设成它们的~省|?/span>reference 也会(x)被设?/span>null——这个过E是一瞬间完成的,对象的内存(sh)(x)被统一地设|成“两进制的?/span>(binary zero)”?strong>如果有对成员变量赋初|则对成员变量q行赋|然后调用基类的构造函数。调用是自动发生的,但是你可以?/span>super 来指定调用哪个构造函?/span>(也就Beetle( )构造函数所做的W一件事)。基cȝ构造过E以?qing)构造顺序,同派生类的相同。基cL造函数运行完毕之后,?x)按照各个变量的字面序q行初始化。最后会(x)执行构造函数的其余部分?/span>



Frank_Fang 2009-05-11 21:16 发表评论
]]>*C++中函数D回的q程中的问题Q是否创Z(f)时变?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/11/270051.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/270051.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/05/11/270051.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/270051.html</wfw:commentRss><trackback:ping>http://www.tkk7.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 /> 怎么可以q回一个局部变量呢Q?Q?Q? <br /> 例如 <br /> Sales_item item1,item2,item3 ; <br /> ......(item2,item3初始化成员变? <br /> item1 = item2 + item3 ; </p> <p><br /> 没有问题Q它q回的是一个|而不是引用,所以是正确的。在item1 = item2 + item3 中发生了(jin)一ơ值拷?赋?Q也是item2+item3q回的局部变量拷贝给?jin)item1之后Q局部变量的作用域结?nbsp;<br />  <br /> <span style="color: red"><strong>可以q回局部变量,但是不能q回局部变量的引用。理解区分值和引用q两个概忉|学习(fn)C++的一大关键,明白q两个概念之后,你就?x)理解?f)什么C++的类里面需要有拯构造函敎ͼ赋值操作符Q析构函C个元素了(jin)以及(qing)其它的一些稀里古怪的用法和忠告了(jin) <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 /> 在return ret 时ƈ不是单的q回q个局部变量,而是q回的是ret的一个副本temp, <br /> temp是通过拯构造函数实现的 ?Sales_item temp(ret);也就是说在主函数中用的其实是个temp,而ret早在operator+调用完毕后就释放?span style="color: red">Q而tempq个 对象一直在dC贮存Q虽然显C是看不到的 Q?strong><span style="color: red"><strong> </strong><strong> ????</strong></span></strong></span></p> <p><br /> 嗯嗯Q(f)时对象一直是个有争议的话题?span style="color: red"><strong>temp只是临时构造的Q在赋值完毕之后它?yu)析构?jin)Q不是一直在dC贮存的。这个(f)时对象的作用域是什么?Q?<br /> </strong></span>在你提的q种情况下,其实~译器是可以优化掉这个副本的Q但是可惜C++标准只允怼?nbsp; “Sales_item item1=item2+item3” q种情况?也就是在拯构造这U情况下Q编译器都不再生副本,而赋D是不行的。也许在未来的C++标准由可能通过拓展语义来消除(f)时对象,毕竟临时对象成ؓ(f)影响C++效率的一个主要因素?<br /> -- </p> <p>???<br /> <span style="color: red"><strong><span style="color: #0000ff">目前的C++标准不允许在赋值的时候优化掉函数q回的副本,也就是下面这两种情况是不一L(fng) <br />  <br /> //q是赋值给item1Q目前的标准下是?x)生副?temp<br /> Sales_item item1,item2,item3; <br /> item1=item2+item3; <br />  <br /> //q是拯构造item1Q编译器通常?x)优?span style="color: #ff0000">(q个优化区别下面所说的NRV优化)</span>掉副本,也就是不产生副本 temp<br /> Sales_item item2,item3; <br /> Sales_item item1=item2+item3; <br /> </span><br /> 看一下代码中的TheFunctionTwo();<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”: 只有?rn)态常量整型数据成员才可以在类中初始化</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重蝲赋D符</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">q个值区别于java不是赋初gؓ(f)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">不能对引用用delete</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好像没有释放内存Q怎么获取的还是原来的?br /> </span><span style="color: #008080"> 81</span> <span style="color: #008000">    </span><span style="color: #008000">//</span><span style="color: #008000">可能在这个内存区域存攄q是原来?先new string后再调用也没变,与编译器有关q是什??</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对象来填充之前的内存区间Q好像没?/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">q时问题来了(jin),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引用?x)发生什么问???</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">q个?x)发生内存泄漏,在函C甌的内存(sh)(x)得不到释?br /> </span><span style="color: #008080"> 97</span> <span style="color: #008000">                                     </span><span style="color: #008000">//</span><span style="color: #008000">myCat2是通过默认的拷贝函数来q行初始化的</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<<"直接的调用默认拷贝构造函数的,q个语句一共创Z(jin)多少个对?<<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">q个然没调用拷贝构造函?????<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">调用默认的赋D符?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<<"调用默认的赋D符?q个语句一共创Z(jin)多少个对?<<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">q种调用的方?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()q回的(f)时对象赋l一个引?<<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<<"打印一下返回的临时对象的地址Q(f)时对象赋l引用之后就不会(x)立即析构?--"<<&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">q个函数在返回时Q是否会(x)创徏一个(f)时引用变???<br /> </span><span style="color: #008080">128</span> <span style="color: #008000">//</span></strong><strong><span style="color: #008000">TheFunctionTwo(SimpleCat &simpleCat)q个函数在传递参数时是否?x)创Z个(f)时的引用变量<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">我要看对象被创徏?jin)几?是否创徏临时对象</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">q回时是否还创徏一个(f)时对?/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">q种方式肯定是错的,q回?jin)局部变量的引用</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">q回时是否还创徏一个(f)时对?已引用的形式</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">我要看再实参对Ş参进行拷贝构造时,是否?x)打印出调用的拷贝函C?/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 /> 先说说第一块吧Q?<br /> q个貌似应该调用拯构造的地方没有调用拯构造,应该是编译器做的优化Q可以参考《深入探索C++对象模型》P66c(din)?<br /> 按照书中的说法,很可能只创徏?个对?<br />  <br /> X bar() <br /> { <br />     X xx; <br />     return xx; <br /> } <br /> 可能?x)被~译器优化成 <br /> void bar(X & _result) <br /> { <br />     _result.X::X(); <br />     return; <br /> } <br />  <br /> 所以调用X a = bar(),其实被{换成 bar(X& a); <br /> 所以一个本该调用拷贝构造的地方却很可能调仅用了(jin)构造函数来完成工作?<br /> q个问题我也没有搞明白,我没有试qlz的代码是不是?x)这P如果真这L(fng)话,我觉得编译器q的太多了(jin)Q如果拷贝构造中有一些特D的功能?像g有个输出语句)Q岂不是无声无息中被Ҏ(gu)?jin)。这个功能被UCؓ(f)NRVQ好像一直没有h对这个问题给出非常明的回答 <br /> ------------------------------------------------------------------------------------------------------------------------------<br /> thxQ在vs2005中代码优化开启时Q第一块确实只?x)调一个构造函?<br /> 把代码优化禁用后Q就?x)调用拷贝构造函C(jin) <br /> 实是个NRV的问?<br /> http://blog.vckbase.com/bruceteen/archive/2005/12/30/16652.html <br /> ------------------------------------------------------------------------------------------------------------------------------<br /> <br /> <span style="color: #ff0000">正如4楼的所说~~~ <br /> 到底创徏多少临时对象Q要视编译器而定Q看~译器是否采用NVR... <br /> (其实一般情况下QNVR都是未采用的~),因此对后三个注释代码?<br /> 如下分析Q写出编译后的伪代码Q仅供参? <br /> ~译器更改后的函数原型void TheFunctionTwo(SimpleCat&) <br /> 注释1Q?<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 /> 注释2Q?<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 /> 注释3Q?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 /> q样q道到底需要多(f)时对象了(jin)~~~~ </span><br /> </span></span> <img src ="http://www.tkk7.com/fhtdy2004/aggbug/270051.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-11 15:22 <a href="http://www.tkk7.com/fhtdy2004/archive/2009/05/11/270051.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>从Java到C++ ?Ҏ(gu)Java与C++~程的不?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/11/270049.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/270049.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/05/11/270049.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/270049.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/fhtdy2004/services/trackbacks/270049.html</trackback:ping><description><![CDATA[  <h2><a title="Permanent Link to 从Java到C++ ?Ҏ(gu)Java与C++~程的不? >从Java到C++ ?Ҏ(gu)Java与C++~程的不?/a></h2> <p>14th August 2007, 11:40 pm </p> <p>原作Q?a target="_blank">Cay Horstmann</a> <a target="_blank">英文原文</a></p> <p>译QAqua  <a title="全文链接" >prglab.com</a></p> <p>注:(x)很多E序员包括本人在内都是先学会(x)的JavaQ然后才学的C++Q其实C++与Java有很多相似和互通之处,有相当的Java知识可以对应转化到C++概念Q从而帮助我们快速上手。这文章介l的内容对从Java向C++转变很有帮助Q所以翻译推荐给有同样需要的朋友。翻译中加入?jin)本人的理解Q不完全是全文照搬。有不明或异议,请参考原文,或留a讨论。还是那句话Q推荐多d版资料?/p> <p>学习(fn)怎样从一U编E语a转移到另一U编E语a是今日的E序员必面对的现实Q还好,C++和Java有很多共同的特点Q所以从Java转到C++容易许多。C++比Java复杂很多Q本文ƈ没打涵盖所有C++的功能。但是如果你能够熟练掌握本文中的所有知识点Q也_有效的用C++?jin)?/p> <p>q里我们只讲Java与C++的不同之处。像程控制(if, while, for)q些在C++与Java中完全一L(fng)内容q里׃讲了(jin)?/p> <p>本文是基于ANSI C++标准的,一些老的C++ ~译器可能不支持q里讲到的一些重要功能。要使用q些~译器,你需要更多学?fn)C++中从Cl承来的部分。那些都出?jin)本文的范畴Q也׃介绍?jin)?/p> <h2>1. 数据cd和变?/h2> <p>C++ 中的变量cd与Java很相伹{像Java一PC++ ?tt><span style="font-size: 12pt; line-height: 150%">int</span></tt>?<tt><span style="font-size: 12pt; line-height: 150%">double</span></tt>cd<strong><span style="color: red">。但是这些数字类型的取D围是依赖于机器的?/span></strong>比如?6位系l上Q例如运行DOS 或Windows 3.x的PCZQ?tt><span style="font-size: 12pt; line-height: 150%">int</span></tt>是双字节(2-byte)的,取D围比Java?-byte?tt><span style="font-size: 12pt; line-height: 150%">int</span></tt>要小很多。在q些机器上,如果 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>cd来更有效的存储数字。(我认为所谓有效是指更高的I间利用率。)(j) 最好是量避免使用q些cd除非是空间利用的有效性对你的pȝ真的非常重要?/p> <p><strong><span style="color: red">在C++中布?yu)(dng)型?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">bool</span></strong></tt><strong><span style="color: red">表示Q而不像在Java中用boolean?/span></strong></p> <p>C++ 中字W串cd?<tt><span style="font-size: 12pt; line-height: 150%">string</span></tt>表示。它与Java中的 <tt><span style="font-size: 12pt; line-height: 150%">String</span></tt>cd非常怼Q但是,q是要逐一以下几点不同之处Q?/p> <p>1. C++ 字符串存储ASCII 码字W,而不是标准码Unicode 字符</p> <p>2. C++ 字符串是可以被修改的Q而Java字符串的内容是不可修改的(immutable)?/p> <p>3. 取子字符串的操作?C++ 中叫?<tt><span style="font-size: 12pt; line-height: 150%">substr</span></tt>Q这个命?tt><span style="font-size: 12pt; line-height: 150%">s.substr(i, n)</span></tt>从字W串s中取得从位置 i 开始长度ؓ(f)n的子字符丌Ӏ?/p> <p>4. <strong><span style="color: red">在C++中,你只能够字W串与其它字W串对象怸?concatenate)Q而不能够与Q意的对象怸联?/span></strong></p> <p>5.<strong><span style="color: red"> C++</strong></span><strong><span style="color: red">中可以直接用关pL作符 </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">来进行字W串比较Q?/span></strong>其中后面四个操作W是按字母顺序进行比较的。这比Java中用函数equals和compareTo来比较要方便很多?/p> <h2>2. 变量和常?/h2> <p>在C++中,本地变量的定义看h与Java中相同,例如Q?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">int n = 5;</span></tt></p> <p><strong><span style="color: red">实际上这正是C++和Java的一个重要不同之处。C++~译器不Ҏ(gu)地变量进行初始化(g)验,所以在C++中很Ҏ(gu)忘记初始化一个变量,q种情况下,变量的D变量所占内存区域中刚好当前存在随机倹{这昄是很Ҏ(gu)产生E序出错的地斏V?/span></strong></p> <p>与Java一P C++中类可以有数据域和静(rn)态变量。不同的是,C++中变量可以在函数甚至是类的外面定义,q些所谓的<strong><span style="color: red">全局变量</span></strong>可以在程序的M函数中被讉KQ因而不易被很好的管理?strong><span style="color: red">所C++中应该尽量避免用全局变量?/span></strong></p> <p>在C++中,帔R可以在Q何地方被定义Q记得在Java中,帔R必须是类的静(rn)态数据static 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">来定义常量,而Java中是 </span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">final</span></strong></tt><strong><span style="color: red">。例如:(x)</span></strong></p> <p><tt><span style="font-size: 12pt; line-height: 150%">const int DAYS_PER_YEAR = 365;</span></tt></p> <h2>3. c?/h2> <p>C++ 中对cȝ定义与Java有些不同Q这里是一个例子:(x)一个C++ 版本?<tt><span style="font-size: 12pt; line-height: 150%">Point</span></tt>c?</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>q里几点重要的不同是Q?/p> <p>1. C++的类定义中分为公共和U有部分Q分别以关键?<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">中类的定义只包含函数的声明,真正的实现另外单独列出?/span></strong></p> <p><strong><span style="color: blue">3. </span><span style="color: blue">讉K函数(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">Q表明这个函C?x)改变本对象的元素倹{?/span></strong></p> <p><strong><span style="color: red">4. </span><span style="color: red">cd义的l尾处有分号</span></strong></p> <p>cM函数的实现跟在类的定义之后。因为函数是在类外面定义的,所以每一个函数的名字前面要加cdUC为前~Qƈ使用<strong><span style="color: red">操作W双冒号::来分割类的名U和函数的名U?/span></strong>。不改变隐含参数|卛_前对象的|(j)的访问函数用 <tt><span style="font-size: 12pt; line-height: 150%">const</span></tt>标明。如下所C是上面cd义中的函数的实现Q?/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的,只需要在变量的名字后面直接赋予构造函数的参数可以了(jin)Q例如:(x)</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>如果不跟参数赋|则用默认构造函敎ͼ例如Q?/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%">q一点与Java很不同。在Java中,q个命o(h)仅仅生成一个没有初始化的对象referenceQ要创徏对象q得使用new在堆中创Z个实际的对象.   </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中,拯一个对象变量只不过是徏立了(jin)另外一个指向对象的reference。拷贝一个C++的对象就像在Java中调用cloneq个函数一P而修Ҏ(gu)贝的g?x)改变原对象的倹{?/span></strong>例如Q?/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 ,q是另外一个新的对象,有自q内存I间*/</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%">Udq而p不动Q即q的值变?sh)(jin),而p的不?/</span></tt></p> <p>多数情况下,C++中这U对象直接对值操作的Ҏ(gu)用v来很方便Q但是也有些时候不如人意Q?/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%">中对象的指针非常cM于Java中的引用</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%">当需要一个函C修改一个对象的|必须C要用按引用调用call by reference (参见下面函数部分)Q或者将参数定义为指针类?在函C通过指针来操作其</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++中实现这U效果,必须使用指针pointerQ参见下面指针部分)(j)</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%">一个对象变量只能存储一U特定的cd的|如果你想要用一个变量来存储不同子类的对象的|多态ploymorphism)Q则需要用指针?/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%">如果你想在C++中用一个变量来或者指向null或者指向一个实际的对象Q则需要用指?/span></strong></p> <h2>5. 函数</h2> <p>在Java中,每一个函数必L者是对象函数(instance method)Q或者是?rn)态函?static function)或称cd数。C++同样支持对象函数和静(rn)态函敎ͼcd敎ͼ(j)Q但同时C++也允许定义不属于Mcȝ函数Q这些函数叫?strong><span style="color: blue">全局函数<em><span style="font-family: 宋体">Qglobal functionsQ?/span></em>?/span></strong></p> <p>特别的是Q每一个C++ E序都从一个叫?<tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">main</span></strong></tt><strong><span style="color: blue">的全局函数</span></strong>开始执行:(x)</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>q有另外一个格式的main函数可以用来捕捉命o(h)行参敎ͼcM于Java的main函数Q但是它要求关于C格式的数l和字符串的知识Q这里就不介l了(jin)?/p> <p>按照?fn)惯Q通常如果E序执行成功Q?<tt><span style="font-size: 12pt; line-height: 150%">main</span></tt>函数q回0Q否则返回非零整数?/p> <p><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">同Java一P函数参数是通过g递的(passed by value)。在Java中,函数无论如何都是可以修改对象(cd?的值的Q?/span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">因ؓ(f)java中传递的是java中的引用,相当与C++中的指针。Java函数q回时也是返回的引用,此引用</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%">Q。然而在C++中,</span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">因ؓ(f)对象直接存储的是实际的?/span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">Q?/span></strong><strong><span style="font-size: 10.5pt; color: blue; line-height: 150%">而不是指向值的referenceQ?/span></strong><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">也就是说传入函数的是一个实际值的拯Q因此也无法修改原来对象的倹{?C++中函数返回时也是要拷贝到外面的变量中?是指针就拯指针Q是对象p行对象的拯)</span></strong></p> <p>所以,C++ 有两U参C递机Ӟ同Java一L(fng)按D?<em><span style="font-family: 宋体">call by value) </em></span>Q以?qing)按地址调用(<em><span style="font-family: 宋体">call by reference</em>)</span>。当一个参数是按reference传递时Q函数可以修改其原始倹{Call by reference 的参数前面有一个地址?<tt><span style="font-size: 12pt; line-height: 150%">&</span></tt>跟在参数cd的后面,例如Q?/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%">在Java中是无法实现q样的功能的</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>来调用这个函敎ͼ则reference参数 <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>的位|,而不是它们的值的拯Q因此这个函数可以实现实际交换这两个参数的倹{?/p> <p><strong><span style="font-size: 10.5pt; color: red; line-height: 150%">?C++中,每当需要实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++ 的向量结构结合了(jin)Java中数l和向量两者的优点。一个C++ 的向量可以方便的被访问,其容量又可以动态的增长。如?<tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>是Q意类型,?<tt><span style="font-size: 12pt; line-height: 150%">vector<T></span></tt>是一个元素ؓ(f) <tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>cd的动态数l。下面的语句</p> <p><tt><span style="font-size: 12pt; line-height: 150%">vector<int> a;</span></tt></p> <p>产生一个初始ؓ(f)I的向量。而语?/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>函数来添加元素:(x)</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>中取出最后一个元素(操作后这个元素被从a中删?Q?使用函数<tt><span style="font-size: 12pt; line-height: 150%">size</span></tt>可以得到当前a中的元素个数?/p> <p>你还可以通过我们熟?zhn)?<tt><span style="font-size: 12pt; line-height: 150%">[]</span></tt>操作W来讉K向量中元素,例如Q?/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>同Java中一P数组索引必须?0 ?tt><span style="font-size: 12pt; line-height: 150%">a.size() - 1</span></tt>之间的倹{但是与Java不同的是QC++中没有runtime的烦(ch)引号合法性检验。试图访问非法的索引位置可能造成非常严重的出错?/p> <p>像所有其?C++ 对象一P向量也是倹{如果你一个向量赋值给另外一个向量变量,所有的元素都会(x)被拷贝过厅R?/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>Ҏ(gu)Java中的情况Q在Java中,一个数l变量是一个指向数l的reference。拷贝这个变量仅仅生另外一个指向同一数组的referenceQ而不?x)拷贝每一个元素的倹{?/p> <p>正因如此Q如果一个C++函数要实C改向量的|必须使用reference参数Q?/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>在C++中,标准的输入输出流用对?<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>操作W写输出Q例如:(x)</p> <p><tt><span style="font-size: 12pt; line-height: 150%">cout << “Hello, World!”;</span></tt></p> <p>也可以连着输出多项内容Q例如:(x)</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>操作W来d一个数字或单词Q例如:(x)</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>可以d整行的输入,例如Q?/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>如果到达输入的结,或者一个数字无法被正确的读入,q个对象会(x)被设|ؓ(f) failed 状态,我们可以使用函数 <tt><span style="font-size: 12pt; line-height: 150%">fail</span></tt>来检验这个状态,例如Q?/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>一旦一个流的状态被设ؓ(f)failedQ我们是很难重置它的状态的Q所以如果你的程序需要处理错误输入的情况Q应该用函?<tt><span style="font-size: 12pt; line-height: 150%">getline</span></tt>然后人工处理得到的输入数据?/p> <h2>8. 指针pointer</h2> <p><strong><span style="color: red">我们已经知道在C++中,对象变量直接存储的是对象的倹{这是与Java不同的,在Java中对象变量存储的是一个地址Q该地址指向对象值实际存储的地方。有时在C++中也需要实现这L(fng)布置Q这qC(jin)指针pointer。在 C++中,一个指向对象的变量叫做指针。如果T是一U数据类型,?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">T*</span></strong></tt><strong><span style="color: red">是指向这U数据类型的指针?/span></strong></p> <p>像 Java中一P一个指针变量可以被初始化ؓ(f)I?<tt><span style="font-size: 12pt; line-height: 150%">NULL</span></tt>Q另外一个指针变量的|或者一个调用new生成的新对象Q?/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++中还有第四种可能Q那是指针可以被初始化为另外一个对象的地址Q这需要用地址操作W?</span></strong><tt><strong><span style="font-size: 12pt; color: red; line-height: 150%">&</span></strong></tt><strong><span style="color: red">Q?/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">q实际上q不是什么好L。保险的做法q是应该直接让指针指向?</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">到目前ؓ(f)止,</span><span style="color: blue">C++ </span><span style="color: blue">指针</span><span style="color: red">看v来非常像 </span><span style="color: blue">Java </span><span style="color: blue">的对象引用变?/span><span style="color: red">。然而,q里有一个很重要的语法的不同?/span></strong>我们必须使用星号操作W?<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>才代表了(jin)q个对象Q?/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>Q?/p> <p><tt><span style="font-size: 12pt; line-height: 150%">(*p).setSalary(91000);</span></tt></p> <p>*p外面的括h必需的,因ؓ(f) <tt><span style="font-size: 12pt; line-height: 150%">.</span></tt>操作W比 * 操作W有更高的优先。C的设计者觉得这U写法很隄Q所以他们提供了(jin)另外一U替代的写法Q?-> 操作W来实现 <tt><span style="font-size: 12pt; line-height: 150%">*</span></tt>?<tt><span style="font-size: 12pt; line-height: 150%">.</span></tt>操作W的l合功能。表辑ּ</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>。你可以单的C<tt><strong><span style="font-size: 12pt; color: blue; line-height: 150%">.</span></strong></tt><strong><span style="color: blue">操作W是在对象上使用的,-> 操作W是在指针上使用的?/span></strong></p> <p><strong><span style="color: blue">如果你不初始化一个指针,或者如果一个指针ؓ(f)I?NULL 或指向的对象不再存在Q则在它上面使用 </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">操作W就?x)出错?不幸的是 C++ runtime pȝq不(g)查这个出错。如果你范了(jin)q个错误Q你的程序可能会(x)行ؓ(f)古怪或L?/span></strong></p> <p><strong> </strong></p> <p>而在Java中,q些错误是不?x)发生的。所有的reference都必d始化Q所有的对象只要仍有reference指向它就不会(x)被从内存?sh)清除,因此你也不?x)有一个指向已被删除的对象的reference。Java的runtime pȝ?x)检查reference是否为空Qƈ在遇到空指针时抛Z个null pointer的例?exception)?/p> <p><strong><span style="color: blue">C++ </span><span style="color: blue">?Javaq有一个显著的不同Q就?Java ?em><span style="font-family: 宋体">垃圾回收</span></em>功能Q能够自动回收被废弃的对象。而在C++中,需要程序员自己理内存分配回收?/span></strong></p> <p>C++中当对象变量出范围时可以自动被回收。但是用new生成的对象必ȝdelete操作W手动删除,例如Q?/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>如果你忘记删除一个对象,那么你的E序有可能最l用光所有内存。这是我们常说的内存泄?(<em><span style="font-family: 宋体">memory leak</em>)</span>。更重要的是Q如果你如果删除?jin)一个对象,然后又l用它Q你可能覆盖不属于你的数据。如果你刚y覆盖?jin)用于处理内存回收的数据域,那么内存分配机制可能运转失常而造成更严重的错误Q而且很难诊断和修复。因此,在C++中最好尽量少用指针?/p> <h2>9. l承</h2> <p>C++和Java中承的基本语法是很怼的。在C++中,使用 <tt><span style="font-size: 12pt; line-height: 150%">: public</span></tt>代替Java中的<tt><span style="font-size: 12pt; line-height: 150%">extends</span></tt>来表C承关p??(C++ 也支持私有承的概念Q但是不太有用?</p> <p>默认情况下,C++中的函数不是动态绑定的。如果你需要某个函数实现动态绑定,需要?tt><span style="font-size: 12pt; line-height: 150%">virtual</span></tt>声明它ؓ(f)虚函敎ͼ例如Q?/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>同Java一P构造函C调用父类的构造函数有Ҏ(gu)的语法?Java使用关键?<tt><span style="font-size: 12pt; line-height: 150%">super</span></tt>。C++中必d子类的构造函C外调用父cȝ构造函数。下面是一个例子:(x)</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 中在子类函数中调用父cȝ函数时也使用关键?<tt><span style="font-size: 12pt; line-height: 150%">super</span></tt>。而在C++中是使用父类的名U加上操作符 <tt><span style="font-size: 12pt; line-height: 150%">::</span></tt>表示Q例如:(x)</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++ 对象变量只能存储特定cd的对象倹{要惛_C++中实现多?polymorphism)Q必M用指针。一?<tt><span style="font-size: 12pt; line-height: 150%">T*</span></tt>指针可以指向cd?<tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>?<tt><span style="font-size: 12pt; line-height: 150%">T</span></tt>的Q意子cȝ对象Q例如:(x)</p> <p><tt><span style="font-size: 12pt; line-height: 150%">Employee* e = new Manager(”Morris, Melinda”, 83000, “Finance”);</span></tt></p> <p>你可以将父类和不同子cȝ对象混合攉C个元素均为指针的向量中,然后调用动态绑定的函数Q如下所C:(x)</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.tkk7.com/fhtdy2004/aggbug/270049.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-11 14:58 <a href="http://www.tkk7.com/fhtdy2004/archive/2009/05/11/270049.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>׃DC++例子惌v的,主要关于C++中的上下转型问题http://www.tkk7.com/fhtdy2004/archive/2009/05/10/269940.htmlFrank_FangFrank_FangSun, 10 May 2009 13:13:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/10/269940.htmlhttp://www.tkk7.com/fhtdy2004/comments/269940.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/10/269940.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/269940.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/269940.html void funOne(Sub &s);
在不向下转型的情况下是否能够直接q样调用funOne(*p)
占位
指针Q引用,对象

Frank_Fang 2009-05-10 21:13 发表评论
]]>
C++cL员变量初始化?qing)与Java中的区别http://www.tkk7.com/fhtdy2004/archive/2009/05/10/269936.htmlFrank_FangFrank_FangSun, 10 May 2009 13:07:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/10/269936.htmlhttp://www.tkk7.com/fhtdy2004/comments/269936.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/10/269936.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/269936.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/269936.html Java中的成员变量的初始化序

C++中的成员变量的初始化序Q主要是内置cd的变量是否给其赋初g(jin)
C++中的内置cdprimitive在全局区域Q注意什么是全局区域)是会(x)被系l自动初始化??,在局部区域是不会(x)被自动初始化?br /> C++中的cȝ型的变量是会(x)被自动初始化的,但要求其有默认的构造函?br /> l承时C++的成员变量与Java中类?

Frank_Fang 2009-05-10 21:07 发表评论
]]>
C++多重l承问题?qing)虚l承virtual的问?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/10/269935.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/269935.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/05/10/269935.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/269935.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/fhtdy2004/services/trackbacks/269935.html</trackback:ping><description><![CDATA[占位 <br /> 多重l承问题,二义性问?br /> sizeof(A)的问?普通承和虚承的差别 <img src ="http://www.tkk7.com/fhtdy2004/aggbug/269935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-10 20:57 <a href="http://www.tkk7.com/fhtdy2004/archive/2009/05/10/269935.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>上一C++虚函C?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/08/269549.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/269549.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/05/08/269549.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/269549.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/fhtdy2004/services/trackbacks/269549.html</trackback:ping><description><![CDATA[    只有注册用户d后才能阅读该文?a href='http://www.tkk7.com/fhtdy2004/archive/2009/05/08/269549.html'>阅读全文</a><img src ="http://www.tkk7.com/fhtdy2004/aggbug/269549.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-08 10:44 <a href="http://www.tkk7.com/fhtdy2004/archive/2009/05/08/269549.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>*虚函数表与虚表指??http://www.tkk7.com/fhtdy2004/archive/2009/05/08/269540.htmlFrank_FangFrank_FangFri, 08 May 2009 02:15:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/08/269540.htmlhttp://www.tkk7.com/fhtdy2004/comments/269540.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/08/269540.html#Feedback2http://www.tkk7.com/fhtdy2004/comments/commentRss/269540.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/269540.html阅读全文

Frank_Fang 2009-05-08 10:15 发表评论
]]>
Java中与C++的又一不同?子类,父类Ҏ(gu)的重载,覆盖Q隐?/title><link>http://www.tkk7.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.tkk7.com/fhtdy2004/archive/2009/05/07/269456.html</guid><wfw:comment>http://www.tkk7.com/fhtdy2004/comments/269456.html</wfw:comment><comments>http://www.tkk7.com/fhtdy2004/archive/2009/05/07/269456.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/fhtdy2004/comments/commentRss/269456.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/fhtdy2004/services/trackbacks/269456.html</trackback:ping><description><![CDATA[    只有注册用户d后才能阅读该文?a href='http://www.tkk7.com/fhtdy2004/archive/2009/05/07/269456.html'>阅读全文</a><img src ="http://www.tkk7.com/fhtdy2004/aggbug/269456.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/fhtdy2004/" target="_blank">Frank_Fang</a> 2009-05-07 17:37 <a href="http://www.tkk7.com/fhtdy2004/archive/2009/05/07/269456.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++ 友元cd友元函数http://www.tkk7.com/fhtdy2004/archive/2009/05/07/269418.htmlFrank_FangFrank_FangThu, 07 May 2009 07:10:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/07/269418.htmlhttp://www.tkk7.com/fhtdy2004/comments/269418.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/07/269418.html#Feedback1http://www.tkk7.com/fhtdy2004/comments/commentRss/269418.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/269418.html阅读全文

Frank_Fang 2009-05-07 15:10 发表评论
]]>
?RTTIҎ(gu)小I?dynamic_cast转换操作W和typeid操作W?http://www.tkk7.com/fhtdy2004/archive/2009/05/07/269414.htmlFrank_FangFrank_FangThu, 07 May 2009 06:20:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/07/269414.htmlhttp://www.tkk7.com/fhtdy2004/comments/269414.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/07/269414.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/269414.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/269414.html阅读全文

Frank_Fang 2009-05-07 14:20 发表评论
]]>
使用c++应注意的地方(一)http://www.tkk7.com/fhtdy2004/archive/2009/05/07/269412.htmlFrank_FangFrank_FangThu, 07 May 2009 06:08:00 GMThttp://www.tkk7.com/fhtdy2004/archive/2009/05/07/269412.htmlhttp://www.tkk7.com/fhtdy2004/comments/269412.htmlhttp://www.tkk7.com/fhtdy2004/archive/2009/05/07/269412.html#Feedback0http://www.tkk7.com/fhtdy2004/comments/commentRss/269412.htmlhttp://www.tkk7.com/fhtdy2004/services/trackbacks/269412.html阅读全文

Frank_Fang 2009-05-07 14:08 发表评论
]]>
վ֩ģ壺 պĻһ| 9420ѸƵ| Ʒר| þþ뾫ƷպĦ | AV˾Ʒһ| **ʵëƬѹۿ| þþ뾫Ʒպ| һëƬѲ| Ļѵַ߹ۿ| ַ߹ۿ| žžƷƵ| þAV뾫Ʒɫҹ| ھƷһëƬѿ| ˳վ| һëƬѹۿշ| ޾ƷۺϾþһ| ߾ƷAAVV| ձ޿һ| ۺһ| 91ƷѾþþþþ| þѾƷav| þþþѿӰƬ| þ޾ƷĻ| ¹͵Ʒվ| 337pձŷ޴| һɫaƬþëƬ| ޹һ߹ۿ| ŮaëƬѹۿ| vƬƵ߹ۿƵ| ƷAVһ| ѵӰ| ޾ƷӰ| Ƶ| ӾƷ| ޾ƷóƬ?V߲| ݺȾƷѹۿ| ۺAVһҳ| Ļѹۿ| av޹av| þþþùƷվ| 91޾Ʒר|