免费人成动漫在线播放r18-免费人成观看在线网-免费人成黄页在线观看日本-免费人成激情视频在线观看冫-jlzzjlzz亚洲大全-jlzzjlzz亚洲日本

二維碼
企資網(wǎng)

掃一掃關(guān)注

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

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

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

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

1) 定義常量

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

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

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

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

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

主要的限制就是只能在 const 類型的對(duì)象上執(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只是替換,是沒(méi)有類型檢查的,容易出錯(cuò)。

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


2) 定義常量指針

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

不可通過(guò)常量指針修改其指向的內(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 ;

不能把常量指針賦值給非常量指針,反過(guò)來(lái)可以

 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) 定義常引用

不能通過(guò)常引用修改其引用的變量

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

const T 類型的常變量和 const T & 類型的引用則不能用來(lái)初始化 T & 類型的引用

T &類型的引用 或 T類型的變量可以用來(lái)初始化 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ā)布者:企資小編個(gè)人觀點(diǎn),本站未對(duì)其內(nèi)容進(jìn)行核實(shí),請(qǐng)讀者僅做參考,如若文中涉及有違公德、觸犯法律的內(nèi)容,一經(jīng)發(fā)現(xiàn),立即刪除,需自行承擔(dān)相應(yīng)責(zé)任。涉及到版權(quán)或其他問(wèn)題,請(qǐng)及時(shí)聯(lián)系我們刪除處理郵件:weilaitui@qq.com。
 

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

粵ICP備16078936號(hào)

微信

關(guān)注
微信

微信二維碼

WAP二維碼

客服

聯(lián)系
客服

聯(lián)系客服:

在線QQ: 303377504

客服電話: 020-82301567

E_mail郵箱: weilaitui@qq.com

微信公眾號(hào): weishitui

客服001 客服002 客服003

工作時(shí)間:

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

反饋

用戶
反饋

主站蜘蛛池模板: yin乱的日子小说 | 色噜噜狠狠一区二区三区 | 亚洲成a人片在线观看中文!!! | 天天插天天色 | 国语性猛交xxxx乱大交 | 国产一级黄色 | 日韩理论在线观看 | 亚洲欧美综合日韩字幕v在线 | 免费国产成人高清在线观看不卡 | 一级特黄aaa大片在 一级特黄aaa大片在线观看 | 国产一区欧美 | 久久成人国产精品青青 | 日本最新在线 | 久99久热 | 六月丁香激情综合成人 | 亚洲欧洲在线观看 | 日韩一级一片 | 欧美日韩国产高清一区二区三区 | videos欧美黑白爆交 | 欧美日韩一级黄色片 | 久久国产精品亚洲一区二区 | 欧美色图综合 | 曰批免费视频播放免费观看网站 | 成人精品一区二区不卡视频 | 中国在线播放精品区 | 成人在线第一页 | 99视频在线观看免费视频 | 日韩中文字幕一在线 | 五月婷婷在线观看视频 | 在线观看不卡 | 国产精品久久久 | 亚洲激情网址 | 亚洲欧美在线视频免费 | 天堂视频在线 | 麻豆精品在线观看 | 久久99国产亚洲精品观看 | 日韩免费福利视频 | 北条麻妃中文字幕在线观看 | 一级女性全黄生活片免费看 | 夜间视频在线观看 | 日韩色视频一区二区三区亚洲 |