• <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>

            統(tǒng)計

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 165636
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            常量函數(shù)、常量引用參數(shù)、常量引用返回值[C++]

            1. 關(guān)于常量引用
            正像在C語言中使用指針一樣,C++中通常使用引用
            有一個函數(shù)
            ... foo()
            并且這個函數(shù)返回一個引用...
            ... & foo()
            ...., 一個指向位圖(Bitmap)的引用 ...
            Bitmap & foo()
            .... 并且這個位圖(bitmap)是常量
            const Bitmap & foo ()

            當(dāng)然你也可以用指針來做同樣的事情:
            const Bitmap * foo()
            foo 返回一個指針 ... 指向一個Bitmap ... 并有這個Bitmap是個常量.
            Bitmap * const foo()
            foo 返回某個東西,這個東西是常量 ... 這個東西又是指針 ... 一個指向Bitmap的指針.
            const Bitmap * const foo()
            foo 返回某個東西,這個東西是常量 ... 這個東西又是指針 ... 一個指向Bitmap的指針.
            ....
            并且這個Bitmap也是常量.
            指針常量與常量指針請參考Blog: http://www.cnblogs.com/JCSU/articles/1019219.html

            const總是針對它左邊的東西起作用, 唯一例外的是,如果const是一個最左邊的標識符,那么const將針對它右邊的東西起作用,因些 const int i; 與 int const i; 意思是相同的.
            原文請參考: http://www.thescripts.com/forum/thread63796.html

            2. 常量函數(shù)、常量引用參數(shù)、常量引用返回值
            例1:bool verifyObjectCorrectness(const myObj &obj); //const reference parameter
            例2:void Add(const int &arg) const; //const function
            例3:IStack const & GetStack() const { return _stack; } //return const reference

            常量引用參數(shù)
            本例中,一個myObj類型的對象obj通過引用傳入函數(shù)verifyObjectCorrectness。為安全起見,使用了const關(guān)鍵字來確保函數(shù)verifyObjectCorrectness不會改變對象obj所引用的對象的狀態(tài)。此外,通過聲明參數(shù)常量,函數(shù)的使用者可以確保他們的對象不會被改變,也不必擔(dān)心在調(diào)用函數(shù)時帶來副作用。以下代碼試圖對聲明為常量引用的形參進行修改,從而不會通過編譯!

            #include <iostream>
            using namespace std;

            class Test
            {
            public:
                
            void f(const int& arg);
            private:
                
            int value; 
            }
            ;
              
            void Test::f(const int& arg) {
                arg=
            10; //試圖修改arg的值,此行將引起編譯器錯誤 //error C2166: l-value specifies const object
                cout<<"arg="<<arg<<endl; 
                value
            =20;
            }
              

            void main()
            {
              
            int i=7;
              Test test;
              test.f(i);
              cout
            <<"i="<<i<<endl; 
            }

            常量函數(shù)
            1. 一個函數(shù)通過在其后面加關(guān)鍵字const,它將被聲明為常量函數(shù)
            2. 在C++,只有將成員函數(shù)聲明為常量函數(shù)才有意義。帶有const作后綴的常量成員函數(shù)又被稱為視察者(inspector),沒
                有const作后綴的非常量成員函數(shù)被稱為變異者(mutator)
            3. 與const有關(guān)的錯誤總是在編譯時發(fā)現(xiàn)
            4. [摘]If the function is not declared const, in can not be applied to a const object, and the compiler will 
                     give an error message. A const function can be applied to a non-const object
            5. 在C++中,一個對象的所有方法都接收一個指向?qū)ο蟊旧淼碾[含的this指針;常量方法則獲取了一個隱含的常量this指針
                void func_name() const;
                以上說明函數(shù)func_name()不會改變*this。當(dāng)你把this指針看成函數(shù)func_name()的一個不可見參數(shù)就理解了
                void func_name(T *this) (no const)
                void func_name(const T *this) (const)
            6. 常量函數(shù)可以被任何對象調(diào)用,而非常量函數(shù)則只能被非常量對象調(diào)用,不能被常量對象調(diào)用,如:

            class Fred {
             
            public:
               
            void inspect() const;   // This member promises NOT to change *this
               void mutate();          // This member function might change *this
             }
            ;
             
             
            void userCode(Fred& changeable, const Fred& unchangeable)
             
            {
               changeable.inspect();   
            // OK: doesn't change a changeable object
               changeable.mutate();    // OK: changes a changeable object
             
               unchangeable.inspect(); 
            // OK: doesn't change an unchangeable object
               unchangeable.mutate();  // ERROR: attempt to change unchangeable object
             }
             

             

            #include <iostream>
            using namespace std;

            struct A
            {
                void
             f() const { cout<<"const function f is called"<<endl; }
                
            void f() { cout<<"non-const function f is called"<<endl; }
                
            void g() { cout<<"non-const function g is called"<<endl; } 
            }
            ;

            void main()
            {
                A a;
                
            const A& ref_a = a;
                a.f(); 
            //calls void f()
                ref_a.f();//calls void f () const
                a.g(); //ok, normal call
                ref_a.g(); //error, const object can not call non-const function
            }

            7. 在類中允許存在同名的常量函數(shù)和非常量函數(shù),編譯器根據(jù)調(diào)用該函數(shù)的對象選擇合適的函數(shù)
                當(dāng)非常量對象調(diào)用該函數(shù)時,先調(diào)用非常量函數(shù);
                當(dāng)常量對象調(diào)用該函數(shù)時,只能調(diào)用常量函數(shù);
                如果在類中只有常量函數(shù)而沒有與其同名的非常量函數(shù),則非常量與常量對象都可調(diào)用該常量函數(shù);如:   

            #include <iostream>
            using namespace std;

            struct A
            {
                
            void f() const { cout<<"const function f is called"<<endl; }
                
            void f() { cout<<"non-const function f is called"<<endl; }
            }
            ;

            void main()
            {
                A a;
                
            const A& ref_a = a;
                a.f(); //calls void f()
                ref_a.f();//calls void f () const
            }

            輸出結(jié)果:
            non-const function f is called
            const function f is called

            #include <iostream>
            using namespace std;

            struct A
            {
                
            void f() const { cout<<"const function f is called"<<endl; }
            }
            ;

            void main()
            {
                A a;
                
            const A& ref_a = a;
                a.f(); 
            //calls void f() const
                ref_a.f();//calls void f () const
            }

            輸出結(jié)果:
            const function f is called
            const function f is called

            8. 以下代碼試圖修改類的數(shù)據(jù)成員,引起編譯錯誤

            #include <iostream>
            using namespace std;

            class Test
            {
            public:
                
            void f(const int& arg) const;
            private:
                
            int value; 
            }
            ;
              
            void Test::f(const int& arg) const{
                
            //arg=10; 
                cout<<"arg="<<arg<<endl; 
                value
            =20; //試圖修改Test的數(shù)據(jù)成員,此行將引起編譯器錯誤 //error C2166: l-value specifies const object
            }
              

            void main()
            {
              
            int i=7;
              Test test;
              test.f(i);
              cout
            <<"i="<<i<<endl; 
            }

            9. const關(guān)鍵字不能用在構(gòu)造函數(shù)與析構(gòu)函數(shù)中。因為構(gòu)造函數(shù)的目的是初始化域值,因此它必須更改對象,析構(gòu)函數(shù)同理

            常量引用返回值
            如果你想從常量方法(函數(shù))中通過引用返回this對象的一個成員, 你應(yīng)該使用常量引用來返回它,即const X&
            也就是說你想通過引用返回的東西如果從邏輯上來講是this對象的一部分(與它是否在物理上嵌入在this對象中無關(guān)),那么常量方法需要通過常量引用或者通過值來返回,而不能通過非常量引用返回
            原文請參考:http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
            class Person {
            public:
                
            const string& name_good() const// Right: the caller can't change the name
                string& name_evil() const;       // Wrong: the caller can change the name
                .
            }
            ;

            void myCode(const Person& p) // You're promising not to change the Person object
            {
                p.name_evil() 
            = "Igor";  // but you changed it anyway!!
            }

            posted on 2009-04-22 23:09 pear_li 閱讀(815) 評論(0)  編輯 收藏 引用 所屬分類: C++

            波多野结衣久久一区二区| 久久亚洲精品人成综合网| 狠狠色综合久久久久尤物| 99久久99久久精品国产| 久久男人中文字幕资源站| 久久久久久久久久久| 九九精品99久久久香蕉| 99久久婷婷国产综合精品草原| 国产 亚洲 欧美 另类 久久| 欧美麻豆久久久久久中文| 亚洲精品乱码久久久久66| 久久AV高潮AV无码AV| 国产Av激情久久无码天堂| 久久久无码精品午夜| 国产情侣久久久久aⅴ免费| 久久久久99精品成人片三人毛片 | A级毛片无码久久精品免费| 久久夜色精品国产亚洲av| 久久久久亚洲精品天堂| 午夜精品久久久久久久无码| 国产精品久久久福利| 国产偷久久久精品专区| 国产午夜精品久久久久九九| AV无码久久久久不卡蜜桃| 国产精品久久久久久久app | 久久精品免费网站网| 精品久久久久中文字幕日本| 久久夜色精品国产噜噜亚洲a| 国内精品久久久久久中文字幕 | 国产激情久久久久影院老熟女| 久久中文字幕无码专区| 99热精品久久只有精品| 国产精品久久久久aaaa| 国内精品人妻无码久久久影院| 亚洲色大成网站WWW久久九九| 亚洲精品乱码久久久久久不卡| 久久久久99精品成人片牛牛影视 | 99久久免费国产精品特黄| 久久久久亚洲AV无码专区桃色| 亚洲欧美精品伊人久久| 久久国产精品成人免费|