• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            huangyi5209

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              0 Posts :: 13 Stories :: 0 Comments :: 0 Trackbacks

            常用鏈接

            留言簿

            我參與的團隊

            搜索

            •  

            最新評論

            Pointers and references look different enough (pointers use the “*” and “->” operators, references use “.“), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other?

            First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must alwaysrefer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.

            “But wait,” you wonder, “what about underhandedness like this?”

            1. char *pc = 0;          // set pointer to null   
            2. char& rc = *pc;        // make reference refer to   
            3.                        // dereferenced null pointer  

            Well, this is evil, pure and simple. The results are undefined (compilers can generate output to do anything they like), and people who write this kind of code should be shunned until they agree to cease and desist. If you have to worry about things like this in your software, you’re probably best off avoiding references entirely. Either that or finding a better class of programmers to work with. We’ll henceforth ignore the possibility that a reference can be “null.”

            Because a reference must refer to an object, C++ requires that references be initialized:

            string& rs;             // error! References must   
            1.                         // be initialized   
            2. string s("xyzzy");   
            3. string& rs = s;         // okay, rs refers to s  

            Pointers are subject to no such restriction:

            1. string *ps;             // uninitialized pointer:   
            2.                         // valid but risky  

            The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That’s because there’s no need to test the validity of a reference before using it:

            void printDouble(const double& rd)   
            1. {   
            2.     cout << rd;         // no need to test rd; it   
            3. }                       // must refer to a double  

            Pointers, on the other hand, should generally be tested against null:

            1. void printDouble(const double *pd)   
            2. {   
            3. if (pd) {             // check for null pointer   
            4. cout << *pd;   
            5. }   
            6. }  

            Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized:

            1. string s1("Nancy");   
            2. string s2("Clancy");   
            3. string& rs = s1;         // rs refers to s1   
            4.   
            5. string *ps = &s1;        // ps points to s1<A name=31186></A>   
            6.   
            7. rs = s2;                 // rs still refers to s1,   
            8.                          // but s1's value is now   
            9.                          // "Clancy"   
            10.   
            11. ps = &s2;                // ps now points to s2;   
            12.                          // s1 is unchanged  

            In general, you should use a pointer whenever you need to take into account the possibility that there’s nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you’re referring to that object, you’ll never want to refer to anything else.

            References, then, are the feature of choice when you know you have something to refer to, when you’ll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers.

            posted on 2011-01-24 20:29 huangyi5209 閱讀(260) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            久久丫精品国产亚洲av不卡 | 久久伊人精品青青草原高清| 久久精品毛片免费观看| 久久福利青草精品资源站| 国产精品无码久久四虎| 亚洲精品乱码久久久久久久久久久久| 久久精品国产亚洲av麻豆小说| 国产999精品久久久久久| 久久久久精品国产亚洲AV无码| 久久精品国产91久久麻豆自制| 精品国产91久久久久久久a| 伊人色综合久久天天人手人婷| 久久夜色tv网站| 国产婷婷成人久久Av免费高清| 亚洲国产婷婷香蕉久久久久久| 潮喷大喷水系列无码久久精品| 四虎国产精品成人免费久久| 日本道色综合久久影院| 色综合久久久久综合体桃花网| 久久久久亚洲av成人无码电影| 久久福利青草精品资源站免费 | 精品国产乱码久久久久久郑州公司| 久久久精品国产Sm最大网站| 久久超碰97人人做人人爱| 亚洲а∨天堂久久精品| 国产精品热久久无码av| 国产精品99久久99久久久| 亚洲AV日韩AV永久无码久久| 伊人热热久久原色播放www| 国产香蕉97碰碰久久人人| 中文字幕一区二区三区久久网站| 久久精品a亚洲国产v高清不卡 | 久久99国产乱子伦精品免费| 久久天天躁狠狠躁夜夜avapp | 久久精品国产亚洲av高清漫画| 欧美国产成人久久精品| 久久SE精品一区二区| 亚洲精品无码久久千人斩| 亚洲日本va中文字幕久久| 色综合久久久久无码专区| 亚洲午夜久久久影院伊人|