﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-凡夫佳佳的瞎说-文章分类-编程日记</title><link>http://www.cppblog.com/jiajiahello/category/9357.html</link><description>佳佳编程记</description><language>zh-cn</language><lastBuildDate>Thu, 29 Dec 2011 23:31:02 GMT</lastBuildDate><pubDate>Thu, 29 Dec 2011 23:31:02 GMT</pubDate><ttl>60</ttl><item><title>【佳佳编程记】-&gt;10.system("pause") 和getchar()有什么区别呢 ？ C++ 心随我向 </title><link>http://www.cppblog.com/jiajiahello/articles/71932.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:20:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71932.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71932.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71932.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71932.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71932.html</trackback:ping><description><![CDATA[<p><font color=#660099 size=4>system("pause") 和getchar()有什么区别呢 ？ <br><br>system("pause") 是调用WINDOWS CONSOLE APP下的命令 PAUSE的。 <br>system("const char*")就是调用WINDOWS CONSOLE APP下的命令。 <br>比如system("exit"); <br>system("ping 192.168.0.1")等等 <br><br>而getchar()只是C标准库里等待一个字符的函数，两者区别很大。 <br><br>/*---------------------------------------------------*/ <br>如何使system("pause") 不弹出&#8220;按任意键继续&#8221;这几个字呢？ <br>用system("pause&gt;nul"):就可以了</font><wbr></p>
  <img src ="http://www.cppblog.com/jiajiahello/aggbug/71932.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:20 <a href="http://www.cppblog.com/jiajiahello/articles/71932.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;09关于"using namespace std;"     C++  心随我向 </title><link>http://www.cppblog.com/jiajiahello/articles/71930.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:19:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71930.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71930.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71930.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71930.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71930.html</trackback:ping><description><![CDATA[<font face=#ce_temp_font#><font size=4><font color=#660099>std 是一个命名空间.. <br>不同的命名空间可以有相同的类名被定义 .. <br>using namespace std; <br>就是指明下面的程序使用std,如果不用这句指明的话就要用std::string(string是std空间中定义的 <br>也可以在全局空间中定义,只要名字空间不一样即可..).. <br>否则可以默认名字空间中有std.便不用std::来修饰 <br>它是C++新标准中有的，解决多人作编大程序时名字冲突问题。比如A B两个班都有叫张三的人,你要使用A班的张三,必然要先指名是A班这个名字空间(namespace),然后你对张三的所有命令才能达到你的预想,不会叫错人 <br>如果你用#include&lt;iostream.h&gt;就不需写这句话（旧标准）。但是如果你用 <br>#include&lt;iostream&gt;就必须要写。 <wbr></font></font></font>
 <img src ="http://www.cppblog.com/jiajiahello/aggbug/71930.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:19 <a href="http://www.cppblog.com/jiajiahello/articles/71930.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;08.C#与VC++联编问题   心随我向  C++</title><link>http://www.cppblog.com/jiajiahello/articles/71929.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:17:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71929.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71929.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71929.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71929.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71929.html</trackback:ping><description><![CDATA[<div id=blogDetailDiv style="FONT-SIZE: 16px"><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//联编是指一个电脑程式自身彼此关联的过程。按照联编所进行的阶段不同，可分为两种不同的联编方法：静态联编和动态联编。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//静态联编 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//静态联编是指联编工作出现在编译连接阶段，这种联编又称早期联编，因为这种联编过程是在程式开始运行之前完成的。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//在编译时所进行的这种联编又称静态束定。在编译时就解决了程式中的操作调用和执行该操作代码间的关系，确定这种关系又称为束定， </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//在编译时束定又称静态束定。下面举一个静态联编的例子。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>#include &lt;cstdlib&gt; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>#include &lt;iostream&gt; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>using namespace std; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>class Point{ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>private: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double x,y;&nbsp;&nbsp;&nbsp;&nbsp; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>public: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Point(double i,double j){x=i;y=j;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double Area() const {return 0.0;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>}; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>class Rectangle:public Point{ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>private: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>double w,h;&nbsp;&nbsp; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>public: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>Rectangle(double i,double j,double k,double l):Point(i,j){ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;w=k; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;h=l; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>double Area() const {return w*h;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>}; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>void fun(Point &amp;s){ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp; cout&lt;&lt;s.Area()&lt;&lt;endl; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>int main(int argc, char *argv[]) </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>{ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;Rectangle rec(3.0,5.2,15.0,25.0); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;fun(rec); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;system("PAUSE"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>system("PAUSE"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>system("PAUSE"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>system("PAUSE"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>system("PAUSE"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>system("PAUSE"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>getchar(); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>&nbsp;&nbsp;&nbsp;&nbsp;return EXIT_SUCCESS; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//该程式的运行结果为：0 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//输出结果表明在fun()函数中，s所引用的对象执行的Area()操作被关联到Point::Area()的实现代码上。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//这是因为静态联编的结果。在程式编译阶段，对s所引用的对象所执行的Area()操作只能束定到Point类的函数上。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//因此，导致程式输出了所不期望的结果。因为我们期望的是s引用的对象所执行的Area()操作应该束定到Rectangl类的Area()函数上。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//这是静态联编所达不到的。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//【动态联编】 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//从对静态联编的上述分析中能够知道，编译程式在编译阶段并不能确切知道将要调用的函数，只有在程式执行时才能确定将要调用的函数， </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//为此要确切知道该调用的函数，需要联编工作要在程式运行时进行，这种在程式运行时进行联编工作被称为动态联编，或称动态束定， </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//又叫晚期联编。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//动态联编实际上是进行动态识别。在上例中，前面分析过了静态联编时，fun()函数中s所引用的对象被束定到Point类上。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//而在运行时进行动态联编将把s的对象引用束定到Rectangle类上。可见，同一个对象引用s，在不同阶段被束定的类对象将是不同的。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//那么如何来确定是静态联编还是动态联编呢？C 规定动态联编是在虚函数的支持下实现的。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//从上述分析能够看出静态联编和动态联编也都是属于多态性的，他们是不同阶段对不同实现进行不同的选择。上例中， </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//实际上是对fun()函数参数的多态性的选择。该函数的参数是个类的对象引用，静态联编和动态联编和动态联编实际上 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099>//是在选择他的静态类型和动态类型。联编是对这个引用的多态性的选择。 </font><wbr></div>
 <img src ="http://www.cppblog.com/jiajiahello/aggbug/71929.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:17 <a href="http://www.cppblog.com/jiajiahello/articles/71929.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;07.虚函数（virtual fun）03       心随我向  C++  </title><link>http://www.cppblog.com/jiajiahello/articles/71928.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:16:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71928.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71928.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71928.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71928.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71928.html</trackback:ping><description><![CDATA[<font color=#660099><font size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><strong><wbr><font style="LINE-HEIGHT: 1.5em" face="" size=6>虚 函 数</font><wbr></strong><wbr></font><font size=4><font color=#660099> <wbr><br><font style="LINE-HEIGHT: 1.5em" face="">&nbsp;&nbsp; 3.以一段代码开始 　</font></font></font><wbr> <br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　&nbsp;&nbsp;#include&lt;iostream&gt; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　using namespace std; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　class A{ //虚函数示例代码2 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　public: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　virtual void fun(){ cout&lt;&lt;"A::fun"&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　virtual void fun2(){cout&lt;&lt;"A::fun2"&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　}; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　class B:public A{ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　public: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void fun(){ cout&lt;&lt;"B::fun"&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void fun2(){ cout&lt;&lt;"B::fun2"&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　}; //end//虚函数示例代码2 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　int main(){ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void (A::*fun)(); //定义一个函数指针 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　A *p=new B; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　fun=&amp;A::fun; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　(p-&gt;*fun)(); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　fun = &amp;A::fun2; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　(p-&gt;*fun)(); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　delete p; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　system("pause"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　你能估算出输出结果吗？如果你估算出的结果是A::fun和A::fun2，呵呵，恭喜恭喜，你中圈套了。其实真正的结果是B::fun和B::fun2，如果你想不通就接着往下看。给个提示，&amp;A::fun和&amp;A::fun2是真正获得了虚函数的地址吗？ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　首先我们回到第二部分，通过段实作代码，得到一个&#8220;通用&#8221;的获得虚函数地址的方法 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　#include&lt;iostream&gt; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　using namespace std; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　//将上面&#8220;虚函数示例代码2&#8221;添加在这里 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void CallVirtualFun(void* pThis,int index=0){ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void (*funptr)(void*); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　long lVptrAddr; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;lVptrAddr,pThis,4); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;funptr,reinterpret_cast&lt;long*&gt;(lVptrAddr)+index,4); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　funptr(pThis); //调用 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　int main(){ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　A* p=new B; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　CallVirtualFun(p); //调用虚函数p-&gt;fun() </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　CallVirtualFun(p,1);//调用虚函数p-&gt;fun2() </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　system("pause"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　现在我们拥有一个&#8220;通用&#8221;的CallVirtualFun方法。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　这个通用方法和第三部分开始处的代码有何联系呢？联系很大。由于A::fun()和A::fun2()是虚函数，所以&amp;A::fun和&amp;A::fun2获得的不是函数的地址，而是一段间接获得虚函数地址的一段代码的地址，我们形象地把这段代码看作那段CallVirtualFun。编译器在编译时，会提供类似于CallVirtualFun这样的代码，当你调用虚函数时，其实就是先调用的那段类似CallVirtualFun的代码，通过这段代码，获得虚函数地址后，最后调用虚函数，这样就真正保证了多态性。同时大家都说虚函数的效率低，其原因就是，在调用虚函数之前，还调用了获得虚函数地址的代码。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　最后的说明：本文的代码可以用VC6和Dev-C++4.9.8.0通过编译，且运行无问题。其他的编译器小弟不敢保证。其中，里面的类比方法只能看成模型，因为不同的编译器的低层实现是不同的。例如this指针，Dev-C++的gcc就是通过压栈，当作参数传递，而VC的编译器则通过取出地址保存在ecx中。所以这些类比方法不能当作具体实现</font><wbr> 
 <img src ="http://www.cppblog.com/jiajiahello/aggbug/71928.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:16 <a href="http://www.cppblog.com/jiajiahello/articles/71928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;06.虚函数（virtual fun）02         心随我向 C++ </title><link>http://www.cppblog.com/jiajiahello/articles/71927.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:15:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71927.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71927.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71927.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71927.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71927.html</trackback:ping><description><![CDATA[<font color=#660099><font size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><strong><wbr><font style="LINE-HEIGHT: 1.5em" face="" size=6>虚 函 数</font><wbr></strong><wbr></font><font size=4><font color=#660099> <wbr><br><font style="LINE-HEIGHT: 1.5em" face="">&nbsp;&nbsp; 2.虚函数是如何做到的（如果你没有看过《Inside The C++ Object Model》这本书，但又急切想知道，那你就应该从这里开始） 　　虚函数是如何做到因对象的不同而调用其相应的函数的呢？现在我们就来剖析虚函数。我们先定义两个类 </font></font></font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　class A{ //虚函数示例代码 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　public: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　virtual void fun(){cout&lt;&lt;1&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　virtual void fun2(){cout&lt;&lt;2&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　}; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　class B:public A{ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　public: </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void fun(){cout&lt;&lt;3&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void fun2(){cout&lt;&lt;4&lt;&lt;endl;} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　}; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　由于这两个类中有虚函数存在，所以编译器就会为他们两个分别插入一段你不知道的数据，并为他们分别创建一个表。那段数据叫做vptr指针，指向那个表。那个表叫做vtbl，每个类都有自己的vtbl，vtbl的作用就是保存自己类中虚函数的地址，我们可以把vtbl形象地看成一个数组，这个数组的每个元素存放的就是虚函数的地址，请看图 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　通过上图，可以看到这两个vtbl分别为class A和class B服务。现在有了这个模型之后，我们来分析下面的代码 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　A *p=new A; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　p-&gt;fun(); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　毫无疑问，调用了A::fun()，但是A::fun()是如何被调用的呢？它像普通函数那样直接跳转到函数的代码处吗？No，其实是这样的，首先是取出vptr的值，这个值就是vtbl的地址，再根据这个值来到vtbl这里，由于调用的函数A::fun()是第一个虚函数，所以取出vtbl第一个slot里的值，这个值就是A::fun()的地址了，最后调用这个函数。现在我们可以看出来了，只要vptr不同，指向的vtbl就不同，而不同的vtbl里装着对应类的虚函数地址，所以这样虚函数就可以完成它的任务。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　而对于class A和class B来说，他们的vptr指针存放在何处呢？其实这个指针就放在他们各自的实例对象里。由于class A和class B都没有数据成员，所以他们的实例对象里就只有一个vptr指针。通过上面的分析，现在我们来实作一段代码，来描述这个带有虚函数的类的简单模型。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　#include&lt;iostream&gt; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　using namespace std; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　//将上面&#8220;虚函数示例代码&#8221;添加在这里 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　int main(){ </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void (*fun)(A*); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　A *p=new B; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　long lVptrAddr; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;lVptrAddr,p,4); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr),4); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　fun(p); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　delete p; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　system("pause"); </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　} </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　用VC或Dev-C++编译运行一下，看看结果是不是输出3，如果不是，那么太阳明天肯定是从西边出来。现在一步一步开始分析 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　void (*fun)(A*); 这段定义了一个函数指针名字叫做fun，而且有一个A*类型的参数，这个函数指针待会儿用来保存从vtbl里取出的函数地址 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　A* p=new B; new B是向内存(内存分5个区:全局名字空间,自由存储区,寄存器,代码空间,栈)自由存储区申请一个内存单元的地址然后隐式地保存在一个指针中.然后把这个地址附值给A类型的指针P. </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　. </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　long lVptrAddr; 这个long类型的变量待会儿用来保存vptr的值 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;lVptrAddr,p,4); 前面说了，他们的实例对象里只有vptr指针，所以我们就放心大胆地把p所指的4bytes内存里的东西复制到lVptrAddr中，所以复制出来的4bytes内容就是vptr的值，即vtbl的地址 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　现在有了vtbl的地址了，那么我们现在就取出vtbl第一个slot里的内容 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr),4); 取出vtbl第一个slot里的内容，并存放在函数指针fun里。需要注意的是lVptrAddr里面是vtbl的地址，但lVptrAddr不是指针，所以我们要把它先转变成指针类型 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　fun(p); 这里就调用了刚才取出的函数地址里的函数，也就是调用了B::fun()这个函数，也许你发现了为什么会有参数p,其实类成员函数调用时，会有个this指针，这个p就是那个this指针，只是在一般的调用中编译器自动帮你处理了而已，而在这里则需要自己处理。 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　delete p; 释放由p指向的自由空间; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　system("pause"); 屏幕暂停; </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　如果调用B::fun2()怎么办？那就取出vtbl的第二个slot里的值就行了 </font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr+4),4); 为什么是加4呢？因为一个指针的长度是4bytes，所以加4。或者memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr)+1,4); 这更符合数组的用法，因为lVptrAddr被转成了long*型别，所以+1就是往后移sizeof(long)的长度</font><wbr> 
 <img src ="http://www.cppblog.com/jiajiahello/aggbug/71927.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:15 <a href="http://www.cppblog.com/jiajiahello/articles/71927.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;05.虚函数（virtual fun）01        C++ </title><link>http://www.cppblog.com/jiajiahello/articles/71926.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:13:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71926.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71926.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71926.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71926.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71926.html</trackback:ping><description><![CDATA[<div id="blogDetailDiv" style="font-size: 16px"><font style="line-height: 1.5em" face="" color="#660099" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font style="line-height: 1.5em" face="" size="6"><strong><wbr>虚 函 数</strong><wbr></font><wbr> <br />　　虚函数联系到多态，多态联系到继承。所以本文中都是在继承层次上做文章。没了继承，什么都没得谈。 <br />　　下面是对C++的虚函数这玩意儿的理解。 <br />　　一， 什么是虚函数（如果不知道虚函数为何物，但有急切的想知道，那你就应该从这里开始） <br />　　简单地说，那些被virtual关键字修饰的成员函数，就是虚函数。虚函数的作用，用专业术语来解释就是实现多态性（Polymorphism），多态性是将接口与实现进行分离；用形象的语言来解释就是实现以共同的方法，但因个体差异而采用不同的策略。下面来看一段简单的代码 <br />　　class A{ <br />　　public: <br />　　void print(){ cout&lt;&lt;&#8221;This is A&#8221;&lt;&lt;endl;} <br />　　}; <br />　　class B:public A{ <br />　　public: <br />　　void print(){ cout&lt;&lt;&#8221;This is B&#8221;&lt;&lt;endl;} <br />　　}; <br />　　int main(){ //为了在以后便于区分，我这段main()代码叫做main1 <br />　　A a; <br />　　B b; <br />　　a.print(); <br />　　b.print(); <br />　　} <br />　　通过class A和class B的print()这个接口，可以看出这两个class因个体的差异而采用了不同的策略，输出的结果也是我们预料中的，分别是This is A和This is B。但这是否真正做到了多态性呢？No，多态还有个关键之处就是一切用指向基类的指针或引用来操作对象。那现在就把main()处的代码改一改。 <br />　　int main(){ //main2 <br />　　A a; <br />　　B b; <br />　　A* p1=&amp;a; <br />　　A* p2=&amp;b; <br />　　p1-&gt;print(); <br />　　p2-&gt;print(); <br />　　} <br />　　运行一下看看结果，哟呵，蓦然回首，结果却是两个This is A。问题来了，p2明明指向的是class B的对象但却是调用的class A的print()函数，这不是我们所期望的结果，那么解决这个问题就需要用到虚函数 <br />　　class A{ <br />　　public: <br />　　virtual void print(){ cout&lt;&lt;&#8221;This is A&#8221;&lt;&lt;endl;} //现在成了虚函数了 <br />　　}; <br />　　class B:public A{ <br />　　public: <br />　　void print(){ cout&lt;&lt;&#8221;This is B&#8221;&lt;&lt;endl;} //这里需要在前面加上关键字virtual吗？ <br />　　}; <br />　　毫无疑问，class A的成员函数print()已经成了虚函数，那么class B的print()成了虚函数了吗？回答是Yes，我们只需在把基类的成员函数设为virtual，其派生类的相应的函数也会自动变为虚函数。所以，class B的print()也成了虚函数。那么对于在派生类的相应函数前是否需要用virtual关键字修饰，那就是你自己的问题了。 <br />　　现在重新运行main2的代码，这样输出的结果就是This is A和This is B了。 <br />　　现在来消化一下，我作个简单的总结，指向基类的指针在操作它的多态类对象时，会根据不同的类对象，调用其相应的函数，这个函数就是虚函数。 </font></div> <img src ="http://www.cppblog.com/jiajiahello/aggbug/71926.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:13 <a href="http://www.cppblog.com/jiajiahello/articles/71926.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;04.动态联编的定义与调用 C++</title><link>http://www.cppblog.com/jiajiahello/articles/71925.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:03:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71925.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71925.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71925.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71925.html</trackback:ping><description><![CDATA[<font color=#660099 size=4>一.定义： <br><wbr><br></font><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　编译程序在编译阶段并不能确切知道将要调用的函数，只有在程序执行时才能确定将要调用的函数，为此要确切知道该调用的函数，要求联编工作要在程序运行时进行，这种在程序运行时进行联编工作被称为动态联编。 <br></font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　动态联编必须包括以下方面： <br></font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　成员函数必须声明为virtual <br></font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　如果基类中声明了为虚函数，则派生类中不必再声明。 <br></font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>二.调用方式： <br></font><wbr><br><font style="LINE-HEIGHT: 1.5em" face="" color=#660099 size=4>　　通过对象的指针或引用调用成员函数；或通过成员函数调用。</font><wbr> 
  <img src ="http://www.cppblog.com/jiajiahello/aggbug/71925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:03 <a href="http://www.cppblog.com/jiajiahello/articles/71925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> 【佳佳编程记】03-&gt;C++必读之取经之路   C++</title><link>http://www.cppblog.com/jiajiahello/articles/71924.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 12:01:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71924.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71924.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71924.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71924.html</trackback:ping><description><![CDATA[<strong><font color="#660066" size="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 学习VC++必读<wbr><wbr><wbr><br /><br /></font></strong><font style="line-height: 1.5em" size="4" face=""><font style="line-height: 1.5em" color="#660066" face="">1.把C++当成一门新的语言学习（和C没啥关系！真的）；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;2.看《Thinking&nbsp;&nbsp; In&nbsp;&nbsp; C++》，不要看《C++变成死相》；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;3.看《The&nbsp;&nbsp; C++&nbsp;&nbsp; Programming&nbsp;&nbsp; Language》和《Inside&nbsp;&nbsp; The&nbsp;&nbsp; C++&nbsp;&nbsp; Object&nbsp;&nbsp; Model》,不要因为他们很难而我们自己是初学者所以就不看；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;4.不要被VC、BCB、BC、TC等词汇所迷惑--他们都是集成开发环境(IDE)，而我们要学的是一门语言；&nbsp;&nbsp;&nbsp;&nbsp;C++builder是Borland公司的C+＋开发环境，VC++是microsoft的。C++builder和VC++都部分地实现了C++标准，也有自己的扩展 . Borland&nbsp;&nbsp; C++和BCB是两种不同观念的开发平台.&nbsp;&nbsp; BCB是RAD(Rapid&nbsp;&nbsp; Application&nbsp;&nbsp; Development快速应用程序开发)平台,主要以VCL(Visual&nbsp;&nbsp; Component&nbsp;&nbsp; Library可视组件库)为主.Borland&nbsp;&nbsp; C++中没有控件的概念.只有封装的类库, BC++用的是owl库．</font><wbr></font><wbr> <br /><br /><font style="line-height: 1.5em" size="4" face=""><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp; sdk&nbsp;&nbsp; 是Software&nbsp;&nbsp; Development&nbsp;&nbsp; Kit&nbsp;&nbsp; ，是为C语言提供的编程工具包&nbsp;&nbsp; </font><wbr><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;api是Application&nbsp;&nbsp; Programming&nbsp;&nbsp; Interface，是windows本身带的一些函数库&nbsp;&nbsp; </font><wbr><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;mfc&nbsp;&nbsp; 是Microsoft&nbsp;&nbsp; Foundation&nbsp;&nbsp; Class&nbsp;&nbsp; ，是vc++中提供的一些类库</font><wbr></font><wbr> <br /><br /><font style="line-height: 1.5em" size="4" face=""><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;5.不要放过任何一个看上去很简单的小编程问题--他们往往并不那么简单，或者可以引伸出很多知识点；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;6.会用Visual&nbsp;&nbsp; C++，并不说明你会C++；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;7.学class并不难，template、STL、generic&nbsp;&nbsp; programming也不过如此--难的是长期坚持实践和不遗余力的博览群书；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;8.如果不是天才的话，想学编程就不要想玩游戏--你以为你做到了，其实你的C++水平并没有和你通关的能力一起变高--其实可以时刻记住：学C++是为了编游戏的；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;9.看Visual&nbsp;&nbsp; C++的书，是学不了C++语言的；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;10.浮躁的人容易说：XX语言不行了，应该学YY；--是你自己不行了吧！？&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;11.浮躁的人容易问：我到底该学什么；--别问，学就对了；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;12.浮躁的人容易问：XX有钱途吗；--建议你去抢银行；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;13.浮躁的人容易说：我要中文版！我英文不行！--不行？学呀！&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;14.浮躁的人容易问：XX和YY哪个好；--告诉你吧，都好--只要你学就行；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;15.浮躁的人分两种：a)只观望而不学的人；b)只学而不坚持的人；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;16.把时髦的技术挂在嘴边，还不如把过时的技术记在心里；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;17.C++不仅仅是支持面向对象的程序设计语言；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;18.学习编程最好的方法之一就是阅读源代码；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;19.在任何时刻都不要认为自己手中的书已经足够了；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;20.请阅读《The&nbsp;&nbsp; Standard&nbsp;&nbsp; C++&nbsp;&nbsp; Bible》(中文版：标准C++宝典)，掌握C++标准；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;21.看得懂的书，请仔细看；看不懂的书，请硬着头皮看；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;22.别指望看第一遍书就能记住和掌握什么--请看第二遍、第三遍；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;23.请看《Effective&nbsp;&nbsp; C++》和《More&nbsp;&nbsp; Effective&nbsp;&nbsp; C++》以及《Exceptional&nbsp;&nbsp; C++》；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;24.不要停留在集成开发环境的摇篮上，要学会控制集成开发环境，还要学会用命令行方式处理程序；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;25.和别人一起讨论有意义的C++知识点，而不是争吵XX行不行或者YY与ZZ哪个好；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;26.请看《程序设计实践》，并严格的按照其要求去做；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;27.不要因为C和C++中有一些语法和关键字看上去相同，就认为它们的意义和作用完全一样；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;28.C++绝不是所谓的C的&#8220;扩充&#8221;--如果C++一开始就起名叫Z语言，你一定不会把C和Z语言联系得那么紧密；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;29.请不要认为学过XX语言再改学C++会有什么问题--你只不过又在学一门全新的语言而已；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;30.读完了《Inside&nbsp;&nbsp; The&nbsp;&nbsp; C++&nbsp;&nbsp; Object&nbsp;&nbsp; Model》以后再来认定自己是不是已经学会了C++；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;31.学习编程的秘诀是：编程，编程，再编程；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;32.请留意下列书籍：《C++面向对象高效编程（C++&nbsp;&nbsp; Effective&nbsp;&nbsp; Object-Oriented&nbsp;&nbsp; Software&nbsp;&nbsp; Construction）》《面向对象软件构造(Object-Oriented&nbsp;&nbsp; Software&nbsp;&nbsp; Construction)》《设计模式（Design&nbsp;&nbsp; Patterns）》《The&nbsp;&nbsp; Art&nbsp;&nbsp; of&nbsp;&nbsp; Computer&nbsp;&nbsp; Programming》；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;33.记住：面向对象技术不只是C++专有的；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;34.请把书上的程序例子亲手输入到电脑上实践，即使配套光盘中有源代码；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;35.把在书中看到的有意义的例子扩充；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;36.请重视C++中的异常处理技术，并将其切实的运用到自己的程序中；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;37.经常回顾自己以前写过的程序，并尝试重写，把自己学到的新知识运用进去；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;38.不要漏掉书中任何一个练习题--请全部做完并记录下解题思路；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;39.C++语言和C++的集成开发环境要同时学习和掌握；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;40.既然决定了学C++,就请坚持学下去，因为学习程序设计语言的目的是掌握程序设计技术，而程序设计技术是跨语言的；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;41.就让C++语言的各种平台和开发环境去激烈的竞争吧，我们要以学习C++语言本身为主；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;42.当你写C++程序写到一半却发现自己用的方法很拙劣时，请不要马上停手；请尽快将余下的部分粗略的完成以保证这个设计的完整性，然后分析自己的错误并重新设计和编写（参见43）；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;43.别心急，设计C++的class确实不容易；自己程序中的class和自己的class设计水平是在不断的编程实践中完善和发展的；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;44.决不要因为程序&#8220;很小&#8221;就不遵循某些你不熟练的规则--好习惯是培养出来的，而不是一次记住的；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;45.每学到一个C++难点的时候，尝试着对别人讲解这个知识点并让他理解--你能讲清楚才说明你真的理解了；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;46.记录下在和别人交流时发现的自己忽视或不理解的知识点；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;47.请不断的对自己写的程序提出更高的要求,哪怕你的程序版本号会变成Version&nbsp;&nbsp; 100.XX；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;48.保存好你写过的所有的程序--那是你最好的积累之一；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;49.请不要做浮躁的人；&nbsp;&nbsp; </font><wbr><br /><br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;50.请热爱C++!</font><wbr></font><wbr> <br /><font style="line-height: 1.5em" color="#660066" face=""><font style="line-height: 1.5em" size="4" face="">附录:</font><wbr> </font><wbr><br /><font style="line-height: 1.5em" color="#660066" face=""><font style="line-height: 1.5em" size="4" face="">gcc和g++都是GNU(组织)的一个编译器。</font><wbr> </font><wbr><br /><font style="line-height: 1.5em" size="4" face=""><font style="line-height: 1.5em" color="#660066" face=""><strong><wbr>误区一:gcc只能编译c代码,g++只能编译c++代码</strong><wbr> </font><wbr><br /><font style="line-height: 1.5em" color="#660066" face="">两者都可以，但是请注意： </font><wbr><br /><font style="line-height: 1.5em" color="#660066" face="">1,后缀为.c的，gcc把它当作是C程序，而g++当作是c++程序；后缀为.cpp的，两者都会认为是c++程序，注意，虽然c++是c的超集，但是两者对语法的要求是有区别的。C++的语法规则更加严谨一些。 </font><wbr><br /><font style="line-height: 1.5em" color="#660066" face="">2,编译阶段，g++会调用gcc，对于c++代码，两者是等价的，但是因为gcc命令不能自动和C＋＋程序使用的库联接，所以通常用g++来完成链接，为了统一起见，干脆编译/链接统统用g++了，这就给人一种错觉，好像cpp程序只能用g++似的。 </font><wbr><br /></font><wbr><font style="line-height: 1.5em" size="4" face=""><br /></font><wbr><font style="line-height: 1.5em" size="4" face=""><strong><wbr><font style="line-height: 1.5em" color="#660066" face="">误区二:gcc不会定义__cplusplus宏，而g++会 </font><wbr><br /></strong><wbr><font style="line-height: 1.5em" color="#660066" face="">实际上，这个宏只是标志着编译器将会把代码按C还是C++语法来解释，如上所述，如果后缀为.c，并且采用gcc编译器，则该宏就是未定义的，否则，就是已定义。 </font><wbr><br /><br /></font><wbr><font style="line-height: 1.5em" size="4" face=""><strong><wbr><font style="line-height: 1.5em" color="#660066" face="">误区三:编译只能用gcc，链接只能用g++ </font><wbr><br /></strong><wbr><font style="line-height: 1.5em" color="#660066" face="">严格来说，这句话不算错误，但是它混淆了概念，应该这样说：编译可以用gcc/g++，而链接可以用g++或者gcc -lstdc++。因为gcc命令不能自动和C＋＋程序使用的库联接，所以通常使用g++来完成联接。但在编译阶段，g++会自动调用gcc，二者等价。 </font><wbr></font><wbr><br /><font style="line-height: 1.5em" color="#660066" face="">（<wbr><a href="http://imgcache.qq.com/#" target="_blank"><u>积分</u></a><wbr>）//--&gt;</font><wbr> <img src ="http://www.cppblog.com/jiajiahello/aggbug/71924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 20:01 <a href="http://www.cppblog.com/jiajiahello/articles/71924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【佳佳编程记】-&gt;02   C++ </title><link>http://www.cppblog.com/jiajiahello/articles/71922.html</link><dc:creator>天天向上</dc:creator><author>天天向上</author><pubDate>Tue, 13 Jan 2009 11:59:00 GMT</pubDate><guid>http://www.cppblog.com/jiajiahello/articles/71922.html</guid><wfw:comment>http://www.cppblog.com/jiajiahello/comments/71922.html</wfw:comment><comments>http://www.cppblog.com/jiajiahello/articles/71922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jiajiahello/comments/commentRss/71922.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jiajiahello/services/trackbacks/71922.html</trackback:ping><description><![CDATA[<strong><font size="6"><font color="#660066">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 【佳佳编程记】<wbr><wbr><wbr></font></font></strong> <br /><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font style="line-height: 1.5em" size="4" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;记录IT生涯的点点滴滴</font><wbr> </font><wbr><br /><font style="line-height: 1.5em" color="#ff0000" face=""><strong><wbr>编者按：</strong><wbr></font><wbr><font style="line-height: 1.5em" color="#000099" face=""><font style="filter: glow(color=#000099,strength=3); display: inline-block; color: #ffffff">戊子元年，初冬有感，日有所思，徘徊于脑中不定，遂想将经历记于此，闲时复续其说，究其完整，将若得志，则将此言编辑成册用以推广；将若不成，则公开分享留作纪念。衷望所作有所值！</font><wbr></font><wbr><font color="#666666"> <br /><br />&nbsp;&nbsp; </font><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp; 学习编程语言也有段时间了，收获是有不少，但似乎进度有点慢。并且发现自己在编程方面缺乏一种精益求精的精神。第一个是前面说到的要精化到编程的各个细节；第二个是对已经编好的程序缺乏想象和继续优化的动力。觉得这是软件设计的两个致命的弱点。以后确实需要改善。</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;对于自己的学习进度，说实话，我不满意我的进度，但我只能做到这样（毕竟是现在）。虽然如此，我感觉这不到一个学期所学的东西比我在专科期间一年学的东西都要多。我觉得效果还是很明显的，学习确实要有可执行的目标计划，才会有条不紊的学到实实在在的东西。在其他人看来，可能觉得我学的这点东西根本算不了什么。要知道，学习不是给别人看的，而是给自己看的，是用来充实和丰富自己的。不求进步神速，只求扎扎实实；不求欢歌笑语，只求安安静静！！！</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#660066" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;到现在为止，C++的基出实验和综合实验基本上已经全部解决了。实验只是考试的必要，但对于自己而言，我需要做的事还很多很多。寒假还要多花时间在C的算法上。</font><wbr><font color="#666666"> <br /><br /><font style="line-height: 1.5em" face=""><strong><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr><wbr><img hspace="3" vspace="3" align="absMiddle" src="http://imgcache.qq.com/qzone/em/e163.gif" width="24" height="24"  alt="" /><wbr></strong><wbr></font><wbr><br /></font><strong><wbr><font style="line-height: 1.5em" color="#cc0000" face="">编程总结：</font><wbr></strong><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><wbr>1.结构体与类的区别：</strong><wbr></font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;结构体是类的特例，所以结构体的所有特性在类中同样适用。但也有所不同：结构体的数据成员在默认情况下是公有成员；而类中的数据成员在默认情况下是私有成员；至于其他的什么成员函数的调用，它们的初始化等基本上完全等价。</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><wbr>2.构造函数的个数：</strong><wbr></font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;原则：以形参的最大个数为准：</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;例：如果最大参数量为2，则构造函数形式有：</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.sample(){ }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 缺省构造函数,默认情况下：i=0,j=0&nbsp;&nbsp;&nbsp;&nbsp;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.sample(int a){ i=a;} // 只带一参，只初始化一个数据成员,初始情况有两种情况</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.sample(int a,int b){ // 全部初始化</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=a;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=b;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;例：如果最大参数量为3，则构造函数形式有： </font><wbr><br /><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.sample(){ }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //形式一&nbsp;&nbsp;缺省构造函数</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.sample(int a){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//形式二，可以任意初始化其中一个数据成员</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=a;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.sample(int a,int b){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//形式三&nbsp;&nbsp;可以任意初始化其中两个数据成员</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=a;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=b;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.sample(int a,int b,int c){//形式四&nbsp;&nbsp;三个数据成员同时初始化</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=a;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=b;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=c;</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp; <strong><wbr>&nbsp;&nbsp;&nbsp;&nbsp;3.虚继承中的相关问题：</strong><wbr></font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;类中的虚函数，不能通过普通对象调用，需用指针对象对其调用。常用于实现程序中的多态，静态联编</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp; <strong><wbr>&nbsp;&nbsp;4.指针悬挂问题：</strong><wbr></font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当类中的数据成员多为指针时容易出现指针悬挂：</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;解决办法一：</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注意分配存储空间和释放存储空间</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#0000cc" face="">个人评价：此方法容易被忽略，运用上较灵活，但是出错率较高</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;解决办法二：</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;定义数据成员时，尽量少使用指针类型，多用数组实现定义</font><wbr><font color="#666666"> <br /></font><font style="line-height: 1.5em" color="#3300ff" face="">个人评价：此方法容易实现，但运用数组没有指针来得灵活，但出错率较低</font><wbr><font color="#666666"> <br /><br /></font><font style="line-height: 1.5em" color="#009900" face="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;时 2008年12月4日0:46:51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>  <img src ="http://www.cppblog.com/jiajiahello/aggbug/71922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jiajiahello/" target="_blank">天天向上</a> 2009-01-13 19:59 <a href="http://www.cppblog.com/jiajiahello/articles/71922.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>