二維碼
        企資網(wǎng)

        掃一掃關(guān)注

        當(dāng)前位置: 首頁 » 企業(yè)資訊 » 熱點(diǎn) » 正文

        C++基礎(chǔ)概念_const關(guān)鍵字

        放大字體  縮小字體 發(fā)布日期:2021-09-14 00:28:07    作者:企資小編    瀏覽次數(shù):63
        導(dǎo)讀

        1) 定義常量有時候我們希望定義這樣一種變量, 它的值不能被改變。為了滿足這一 要求, 可以用關(guān)鍵字 const 對變量的類型加以限定。const對象一旦創(chuàng)建后其值就不能再改變,所以const對象必須在定義的時候就初始化。

        1) 定義常量

        有時候我們希望定義這樣一種變量, 它的值不能被改變。

        為了滿足這一 要求, 可以用關(guān)鍵字 const 對變量的類型加以限定。

        const對象一旦創(chuàng)建后其值就不能再改變,所以const對象必須在定義的時候就初始化。

        初始值可以是任意復(fù)雜的表達(dá)式。

        與非const類型所能參與的操作相比,const 類型的對象能完成其中大部分;

        主要的限制就是只能在 const 類型的對象上執(zhí)行不改變其內(nèi)容的操作。


        // const int max ; //error: uninitialized const 'max' const int max = 100 ; cout << max << endl ; int j = max ; cout << j << endl ;// max = j ; //error: assignment of read-only variable 'max'


        const定義常量與define的區(qū)別:

        const是有類型的,可以做類型檢查;define只是替換,是沒有類型檢查的,容易出錯。

        所以在C++推薦的做法是使用const,不推薦使用define


        2) 定義常量指針

        const T *用于定義常量指針。

        不可通過常量指針修改其指向的內(nèi)容

         int n = 4 ; const int *p = &n ;// *p = 5 ; // error: assignment of read-only location '* p'

        常量指針指向的位置可以變化

         int n = 4 ; const int *p = &n ; n =5 ; int m = 6 ; p = &m ; cout << *p << endl ;

        不能把常量指針賦值給非常量指針,反過來可以

         int n = 4 ; const int *p = &n ; n =5 ; int m = 6 ; p = &m ; int * p2 = &n ; p = p2 ; // no error cout << *p << endl ;// p2 = p ; //error: invalid conversion from 'const int*' to 'int*'

        3) 定義常引用

        不能通過常引用修改其引用的變量

         const int &r = m ; cout << r << endl ;// r = n ; //error: assignment of read-only reference 'r'

        const T 類型的常變量和 const T & 類型的引用則不能用來初始化 T & 類型的引用

        T &類型的引用 或 T類型的變量可以用來初始化 const T & 類型的引用。

         int & r = m ; const int &t = r ;// int & y = t ; // error: binding reference of type 'int&' to 'const int' discards qualifiers

        參考代碼:

        #include <iostream>#include <cstdio>using namespace std;int main (){// const int max ; //error: uninitialized const 'max' const int max = 100 ; cout << max << endl ; int j = max ; cout << j << endl ;// max = j ; //error: assignment of read-only variable 'max' int n = 4 ; const int *p = &n ;// *p = 5 ; // error: assignment of read-only location '* p' cout << *p << endl ; n =5 ; cout << *p << endl ; int m = 6 ; p = &m ; cout << *p << endl ; int * p2 = &n ; p = p2 ; cout << *p << endl ;// p2 = p ; //error: invalid conversion from 'const int*' to 'int*' int & r = m ; const int &t = r ; cout << t << endl ;// t = n ; //error: assignment of read-only reference 't'// int & y = t ; // error: binding reference of type 'int&' to 'const int' discards qualifiers return 0 ;}


        輸出結(jié)果:

        g++ const_pointer.cpp./a.out10010045656
         
        (文/企資小編)
        免責(zé)聲明
        本文僅代表作發(fā)布者:企資小編個人觀點(diǎn),本站未對其內(nèi)容進(jìn)行核實(shí),請讀者僅做參考,如若文中涉及有違公德、觸犯法律的內(nèi)容,一經(jīng)發(fā)現(xiàn),立即刪除,需自行承擔(dān)相應(yīng)責(zé)任。涉及到版權(quán)或其他問題,請及時聯(lián)系我們刪除處理郵件:weilaitui@qq.com。
         

        Copyright ? 2016 - 2025 - 企資網(wǎng) 48903.COM All Rights Reserved 粵公網(wǎng)安備 44030702000589號

        粵ICP備16078936號

        微信

        關(guān)注
        微信

        微信二維碼

        WAP二維碼

        客服

        聯(lián)系
        客服

        聯(lián)系客服:

        在線QQ: 303377504

        客服電話: 020-82301567

        E_mail郵箱: weilaitui@qq.com

        微信公眾號: weishitui

        客服001 客服002 客服003

        工作時間:

        周一至周五: 09:00 - 18:00

        反饋

        用戶
        反饋

        主站蜘蛛池模板: 日韩免费观看一区| 成人免费一区二区无码视频| 亚洲一区二区三区乱码在线欧洲| 国产麻豆剧果冻传媒一区| 曰韩精品无码一区二区三区| 精品人妻少妇一区二区三区不卡| 日韩高清国产一区在线| 国模少妇一区二区三区| 精品福利一区二区三区精品国产第一国产综合精品 | 亚洲电影唐人社一区二区| 精品久久久久中文字幕一区| 国产在线无码视频一区| 国产一区二区三区乱码在线观看| 亚洲福利一区二区精品秒拍| 美女一区二区三区| 日本免费电影一区二区| 国产视频一区二区在线播放| 成人免费区一区二区三区| 国产一区二区三精品久久久无广告| 一区 二区 三区 中文字幕| 精品在线视频一区| 国产精品成人一区二区| 亚洲人成网站18禁止一区| 一区二区高清在线观看| 精品亚洲一区二区三区在线观看| 亚洲av片一区二区三区| 岛国精品一区免费视频在线观看| 区三区激情福利综合中文字幕在线一区| 国产精品成人99一区无码| 白丝爆浆18禁一区二区三区| 视频在线一区二区三区| 国产在线精品一区二区夜色| 亚洲色大成网站www永久一区| 亚洲V无码一区二区三区四区观看| 亚洲国产精品乱码一区二区| 亚洲色精品VR一区区三区| 日本一区二区三区日本免费| 国产成人精品一区二三区在线观看| 亲子乱av一区二区三区| 在线中文字幕一区| 国产第一区二区三区在线观看 |