欧美日韩午夜精品不卡综合-欧美日韩系列-欧美日韩小视频-欧美日韩性-成人五月网-成人五级毛片免费播放

二維碼
企資網

掃一掃關注

當前位置: 首頁 » 企業(yè)資訊 » 熱點 » 正文

C++基礎概念_const關鍵字

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

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

1) 定義常量

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

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

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

初始值可以是任意復雜的表達式。

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

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


// 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 *用于定義常量指針。

不可通過常量指針修改其指向的內容

 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 ;}


輸出結果:

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

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

粵ICP備16078936號

微信

關注
微信

微信二維碼

WAP二維碼

客服

聯系
客服

聯系客服:

在線QQ: 303377504

客服電話: 020-82301567

E_mail郵箱: weilaitui@qq.com

微信公眾號: weishitui

客服001 客服002 客服003

工作時間:

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

反饋

用戶
反饋

主站蜘蛛池模板: 一级特黄色毛片免费看 | 91国在线视频 | 欧美成人aaaa免费高清 | avtt天堂网永久资源手机版 | 成人性动漫高清免费观看网址 | 91视频国内| 欧美激情一区二区三区高清视频 | 女人被男人躁得好爽免费文 | 大片国产片日本观看免费视频 | 久草在线国产 | 看美女毛片| 91综合精品网站久久 | 国产成人tv在线观看 | 成人精品在线观看 | 99国产精品高清一区二区二区 | 性做久久久久免费观看 | 男人的天堂在线免费视频 | 欧美日韩在线视频观看 | 欧美日韩精品一区二区三区视频 | 国产好片无限资源 | 欧美69视频在线 | 色综合久久综合 | 最新亚洲精品国自产在线观看 | 国产成人精品magnet | 成人9久久国产精品品 | 欧美人成人亚洲专区中文字幕 | 看真人一一级毛片 | 亚洲国产精品67194成人 | 日韩欧美在线一级一中文字暮 | a级毛片免费| 91精品亚洲| 91亚洲人成手机在线观看 | 日本三级香港三级网站 | 日本成本人片 | 精品视频一区二区三区免费 | 亚洲一区二区久久 | 欧美一级毛片香蕉网 | 亚洲欧美精品 | 精品国产一区二区三区国产馆 | 国产精品18久久久久久久久久 | 亚洲精品一区二区综合 |