• <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>
            const主要是為了程序的健壯型,減少程序出錯.
            最基本的用法:
            const int a=100; b
            的內(nèi)容不變,b只能是100也就是聲明一個int類型的常量(#define b =100)
            int const b=100; //
            和上面作用一樣 

            const
            指針和引用一般用在函數(shù)的參數(shù)中
            int* m = &a; //
            出錯,常量只能用常指針
            int c= 1;const int*pc = &c;//
            常指針可指向常量 

            const int* pa = &a; //
            指針指向的內(nèi)容為常量(就是b的值不變)
            int const *a = &b; //
            指針指向的內(nèi)容為常量(就是b的值不變)*p=3//error
            int* const a = &b; //
            指針為常量,不能更改指針了如 a++但可以改值*p=3; 

            從這可以看出const放在*左側(cè)修飾的是指針的內(nèi)容,const放在*右側(cè)修飾的是指針
            本身

            const
            引用的用法和指針一樣
            int const & a=b;
            和指針一樣
            const int& a=b;
            和指針一樣
            但沒有 int& const a=b 的用法因?yàn)橐貌荒茏鲆莆贿\(yùn)算,但只是出個warning 

            const int* const a = &b; //
            綜合應(yīng)用,一般用來傳遞多維的數(shù)組
            類如:char* init[] = {"Paris","in the","Spring"};
            void fun(const int* const a){}
            fun(init)//
            保護(hù)參數(shù)不被修改 

            int A(int)const; //
            是常函數(shù),只能用在類中,調(diào)用它的對象不能改改變成員值
            const int A(); //
            返回的是常量,所以必須這么調(diào)用 cosnt int a=A();
            int A(const int); //
            參數(shù)不能改值,可用在任意函數(shù)
            int A(const int*);
            ....
            int height() const;//
            常函數(shù)只能由常函數(shù)調(diào)用
            int max(int,int) const;
            int Max = max(height(),height()); 

            const int* pHeap = new int;
            delete pHeap;
            p = NULL;//
            出錯
            我的解決辦法是強(qiáng)制類型轉(zhuǎn)換
            const int* pHeap = new int(1);
            delete (int*)pHeap;
            pHeap = NULL; 

            一、const 和引用聯(lián)合使用的時(shí)候要注意 

            const int a = 1; 
            const int& ref1 = a;
            const int& ref2 = 1; 

            ref1
            ref2 都是正確的,但是他們引用的內(nèi)容和一般的引用不同
            const int& ref1 = a; 而言,其實(shí)這個 ref1 已經(jīng)和 a 沒有任何關(guān)系了
            ref1
            實(shí)際上是對一個臨時(shí)量的引用。同理 const int& ref2 = 1; 也是對
            一個臨時(shí)量做的引用。當(dāng)引用臨時(shí)量是 C++ 的隱式類型轉(zhuǎn)換可以起作用。
            臨時(shí)量的生存期和引用量的生存期相同。 

            二、強(qiáng)傳const對象可能導(dǎo)致無定義行為 

            對于優(yōu)化做的比較好的編譯器,代碼 const int i = 1;
            當(dāng)后面用到變量 i 的時(shí)候,編譯器會優(yōu)化掉對 i 的存取,而直接使用立即數(shù)

            const int i = 1; 

            *(const_cast<int*>(&i)) = 2;
            cout << *(int*)&i << endl;
            cout << i << endl; 

            所以,對 const 對象做 const_cast 可能導(dǎo)致無定義行為
            目前我就遇到這些問題,那位還有補(bǔ)充的嗎 





            能不能把自己的經(jīng)驗(yàn)也談?wù)劇4蠹医涣鹘涣?SPAN lang=EN-US>
            這個就是我在調(diào)錯時(shí)發(fā)現(xiàn)的
            int height() const;//
            常函數(shù)只能由常函數(shù)調(diào)用
            int max(int,int) const;
            int Max = max(height(),height()); 





            Thinking again in C++
            (一)常量性原理 cphj(原作) 
            有些地方很受啟發(fā) 


            1.
            不能將const修飾的任何對象、引用和指針作為賦值表達(dá)式的左值。
            const int cx=100;
            const int & rcx=cx;
            const int * pcx=&cx;
            cx=200; //error
            rcx=200; //error
            *pcx=200; //error 

            2.const
            類型的對象不能直接被non-const類型的別名所引用。
            (1)
            不能將const類型的對象傳遞給non-const類型的引用。
            const int cx=100;
            int & rx=cx; //error
            (2)
            不能將const類型的實(shí)參傳遞給形參為non-const類型引用的函數(shù)。
            void f(int a)
            {
            }
            void g(int & ra)
            {
            }
            const int cx=100;
            f(cx); //ok
            g(cx); //error
            (3)
            不能將const類型的對象作為non-const類型引用的函數(shù)返回值。
            int & f(const int & rca)
            {
            return rca; //error
            }
            int x=100;
            f(x); 

            3.
            可以使用const類型別名引用non-const對象。此時(shí)通過const引用不能修改對象,但對象可以通過non-const引用被修改。
            int x=100;
            int & rx=x;
            const int & rcx=x; //ok
            x=200;
            rx=200;
            rcx=200; //error 

            4.
            指針的屬性有兩個:指針的類型和指針本身的常量性。其中,指向const對象與指向non-const對象,是不同的指針類型。
            int x=100;
            const int * pcx=&x; //[1]
            int * px=&x; //[2]
            int y=100;
            int * const cpy=&y; //[3]
            int * py=&y; //[4]
            [1][2]
            兩個指針的類型不同;[3][4]兩個指針的常量性不同。
            對象與指向?qū)ο蟮闹羔樀囊?guī)則類似于對象與引用。即,const類型的對象不能直接被non-const類型的指針?biāo)甘荆ㄍ?SPAN lang=EN-US>2);可以使用const類型的指針指向non-const對象(同3)。 

            5.
            可以將相同類型(包括常量性)的const指針值賦給non-const指針。
            int x=100;
            int * px;
            const int * pcx=&x;
            px=pcx; //error
            int * const cpx=&x;
            px=cpx; //ok 

            6.
            若函數(shù)的返回值為內(nèi)建類型或是指針,則該返回值自動成為const性質(zhì)。但自定義類型則為non-const性質(zhì)。
            int f() //
            相當(dāng)于返回const int
            {
            return 100;
            }
            int * g(int & ra) //
            相當(dāng)于返回int * const
            {
            return &ra;
            }
            class CTest
            {
            int n;
            public:
            CTest(int n){this->n=n;}
            };
            CTest h() //
            返回的就是CTest
            {
            return CTest(200);


            f()=200; //error 

            int x=100;
            int y=200;
            int * px=&x;
            g(y)=px; //error
            *g(y)=x; //ok
            ,從這點(diǎn)可以看出g()返回的不是const int * 

            CTest t(100);
            h()=t; //ok
            ,但卻是完全錯誤的、危險(xiǎn)的做法
            //
            所以h()的正確寫法是返回const CTest





            const int b=100; b
            的內(nèi)容不變,b只能是100
            int const b=100; b
            必須為int,不能為其他類型?
            2句話的意思應(yīng)該是一樣的吧 , THINKING IN C++是這樣說的





            const int a=100; a
            的內(nèi)容不變,a只能是100(同樣不能類型轉(zhuǎn)換)。
            int const b=100; b
            必須為int,不能為其他類型?(同樣在使用中不能修改)。
            所以ab是一樣的,稱為整型常數(shù),在使用中不能被修改,當(dāng)然都不能轉(zhuǎn)為其他類型了。 
            #include <iostream> 

            using namespace std; 

            int main()
            {
              const int a = 100;
              int const b = 100; 

              a = 100; //這四條語句編譯時(shí)都會出現(xiàn)“Cannot modify a const object 
            b = 100; //in function main()
            ”的錯誤提示,也就是說,任何企圖修改   a = 100.0; //ab(其實(shí)是一樣的)的行為都會出現(xiàn)“災(zāi)難”,在語法上講就  b = 100.0; //ab都不能出現(xiàn)在賦值語句的左邊! 

              cout<<'\n'<<a<<'\n'<<b<<endl; 

              return 0;
            }





            常函數(shù)的調(diào)用是這樣的:常量對象只能調(diào)用常成員函數(shù),非常量對象即可以調(diào)常成員函數(shù),也可以調(diào)一般成員函數(shù),但當(dāng)某個函數(shù)有const和非const兩個版本時(shí),const對象調(diào)const版本,非const對象調(diào)非const版本
            例:
            class A
            {
            public:
            int & GetData(){return data;}
            const int & GetData()const {return data;}
            private:
            int data;

            A a;
            a.GetData();//
            調(diào)用int & GetData(){return data;}
            //
            但如果沒有這個函數(shù),也可以調(diào)用const int & GetData()const 
            const A const_a;
            const_a.GetData();//
            調(diào)用const int & GetData()const {return data;}
            常函數(shù)只能調(diào)常函數(shù),也是由于這個原因





            算你狠!加兩點(diǎn)

            一、const 和引用聯(lián)合使用的時(shí)候要注意 

            const int a = 1; 
            const int& ref1 = a;
            const int& ref2 = 1; 

            ref1
            ref2 都是正確的,但是他們引用的內(nèi)容和一般的引用不同
            const int& ref1 = a; 而言,其實(shí)這個 ref1 已經(jīng)和 a 沒有任何關(guān)系了
            ref1
            實(shí)際上是對一個臨時(shí)量的引用。同理 const int& ref2 = 1; 也是對
            一個臨時(shí)量做的引用。當(dāng)引用臨時(shí)量是 C++ 的隱式類型轉(zhuǎn)換可以起作用。
            臨時(shí)量的生存期和引用量的生存期相同。 

            二、強(qiáng)傳const對象可能導(dǎo)致無定義行為 

            對于優(yōu)化做的比較好的編譯器,代碼 const int i = 1;
            當(dāng)后面用到變量 i 的時(shí)候,編譯器會優(yōu)化掉對 i 的存取,而直接使用立即數(shù)

            const int i = 1; 

            *(const_cast<int*>(&i)) = 2;
            cout << *(int*)&i << endl;
            cout << i << endl; 

            所以,對 const 對象做 const_cast 可能導(dǎo)致無定義行為





            #include <iostream.h>
            void fun(char b){cout <<"void"<<endl;}
            int fun(int const b){cout <<"int"<<endl;} 
            int main()
            {
            fun(1.0);//
            詳細(xì)看看重載函數(shù)吧 
            fun(4); //
            想一想調(diào)用哪一個 

            return 0;
            }
            我試了一下,會出錯? vc說:'fun':ambiguous call to overloaded function 





            補(bǔ)充的好啊,這個一般不會注意的
            const int i = 1;
            *(const_cast<int*>(&i)) = 2;
            cout << *(int*)&i << endl;
            cout << i << endl;
            這個可真有意思,調(diào)試時(shí)兩個都是2,可編譯就是21
            const
            的永遠(yuǎn)都是const,這樣能更改就不錯了,不然就自相矛盾了
            奇怪的是 pi &i地址一樣啊,就像樓上說的這是編譯時(shí)的優(yōu)化
            處理
            const int i = 1;
            int* pi=const_cast<int*>(&i);
            *pi=2;
            cout << *pi << endl;
            cout << i << endl; 





            那個主要是隱式轉(zhuǎn)換
            你可依次把兩個函數(shù)注掉看看調(diào)用
            #include <iostream.h>
            //void fun(char b){cout <<"void"<<endl;}
            void fun(int b){cout <<"int"<<endl;}
            int main()
            {
            fun('a');
            fun(4); 
            return 0;
            }
            Posted on 2005-12-09 10:54 艾凡赫 閱讀(47975) 評論(13)  編輯 收藏 引用 所屬分類: 基礎(chǔ)知識

            Feedback

            # re: const用法  回復(fù)  更多評論   

            2007-10-05 22:25 by 金鵬
            前輩啊,我初學(xué)者,看不懂你們在說什么啊.怎么辦啊!

            # re: const用法  回復(fù)  更多評論   

            2007-10-09 09:01 by ivenher
            多多嘗試,有了手感就好了

            # re: const用法  回復(fù)  更多評論   

            2007-12-27 21:48 by 張飛
            @金鵬
            蠢豬!

            # re: const用法  回復(fù)  更多評論   

            2008-01-17 14:57 by harer
            很好,收藏了。
            謝謝。

            # re: const用法  回復(fù)  更多評論   

            2008-02-08 20:22 by 敖馬
            不得不說博客主人談的很細(xì)致,看得出來經(jīng)驗(yàn)豐富啊~
            作為一個初學(xué)C++的小毛孩,我對C++還沒"感覺",不過編程就是這樣,入門容易,精通難,本身的多讀多練很重要.當(dāng)然了,能看到前輩這樣的經(jīng)驗(yàn)之談也很有幫助,作為晚輩的我們說不定哪天遇到了問題就可以少走一點(diǎn)彎路~

            謝了哈~~^_^

            # re: const用法[未登錄]  回復(fù)  更多評論   

            2008-03-20 00:21 by me
            發(fā)現(xiàn)其中有不少錯誤

            # re: const用法  回復(fù)  更多評論   

            2008-04-29 16:15 by 程誠成
            今天又復(fù)習(xí)了一遍,收藏了,很全面。

            # re: const用法  回復(fù)  更多評論   

            2008-07-26 16:04 by 啊啊
            說的太深奧了

            # re: const用法  回復(fù)  更多評論   

            2009-01-09 15:24 by bigcoder
            貌似其中有錯誤

            # re: const用法  回復(fù)  更多評論   

            2009-03-31 12:31 by akxxx
            錯誤很多。。。。

            # re: const用法  回復(fù)  更多評論   

            2009-10-22 14:59 by kakayunmu
            很好很詳細(xì)

            # re: const用法  回復(fù)  更多評論   

            2009-11-21 14:40 by alan
            錯誤很多 COPY的吧。。。

            # re: const用法[未登錄]  回復(fù)  更多評論   

            2010-05-07 17:50 by a
            const int A(); //返回的是常量,所以必須這么調(diào)用 cosnt int a=A();
            這里錯了!
            直接這樣調(diào)用就行
            int a = A();
            国产精品免费看久久久香蕉| 亚洲精品无码成人片久久| 综合久久国产九一剧情麻豆| 香蕉aa三级久久毛片| 天天做夜夜做久久做狠狠| 午夜福利91久久福利| 无码任你躁久久久久久老妇App| 久久综合九色欧美综合狠狠 | 四虎国产精品免费久久久| www久久久天天com| 91精品国产91热久久久久福利| 青青国产成人久久91网| 7国产欧美日韩综合天堂中文久久久久 | 青青草原综合久久| 久久久久国产精品嫩草影院| 欧美日韩精品久久久久| 久久久久久亚洲精品影院| 久久精品国产99久久久| 伊人色综合久久天天| 久久综合色区| 久久精品亚洲一区二区三区浴池 | 大香伊人久久精品一区二区| 久久久一本精品99久久精品88| 精品久久久久香蕉网| 亚洲午夜久久久精品影院| 性欧美大战久久久久久久| 激情伊人五月天久久综合| 国产免费久久精品99久久| 一本久道久久综合狠狠爱| 99久久精品国产综合一区| 久久人人爽人人爽人人av东京热| 精品久久久久久国产潘金莲| 久久精品亚洲福利| 麻豆亚洲AV永久无码精品久久| 国产视频久久| 久久A级毛片免费观看| 三级片免费观看久久| 天天综合久久久网| 久久天天躁狠狠躁夜夜躁2O2O| 久久亚洲中文字幕精品一区| 精品久久久久久中文字幕|