C++中const的特性的使用
目錄(作用):
1:修飾變量,說(shuō)明該變量不可以被改變;
2:修飾指針,分為只想常量的指針和自身是常量的指針
3:修飾引用,指向常量的引用,用于修飾形參,即避免了拷貝,有避免了函數(shù)對(duì)值的修改;
4:修改成員函數(shù):說(shuō)明該成員函數(shù)內(nèi)不能修改成員變量。
5:指針與引用
正文:
以下是對(duì)各種情況的示例:
//注:1:const修飾的引用cj的值且引用的對(duì)象無(wú)法修改無(wú)法修改,但是引用的i是可修改的
#include <iostream>
using namespace std;
int main() {
int i = 1;
const int &cj = i;
cout << "cj : " <<cj<< endl;(√)
i=2;
cout << "cj : " <<cj<< endl;(√)
cj=3;
cout << "cj : " <<cj<< endl;(×)
int a=9;
cj=a; (×)
return 0;
}
錯(cuò)誤提示:
/code/main.cpp: In function ‘int main()':
/code/main.cpp:15:4: error: assignment of read-only reference ‘cj'
cj=3;
^
/code/main.cpp:19:4: error: assignment of read-only reference ‘cj'
cj=a;
^
sandbox> exited with status 0
//注:常量引用,本身也要是常量才行:
#include <iostream>
using namespace std;
int main() {
const int i = 4;
const int &ck = i; //正確,常量對(duì)象綁定到 const引用
cout<< "ck="<<ck<<endl;
const int b = 5;
int &r = b; //錯(cuò)誤,
return 0;
}
/code/main.cpp: In function ‘int main()':
/code/main.cpp:13:14: error: invalid initialization of reference of type ‘int&' from expression of type ‘const int'
int &r = b; //錯(cuò)誤,
^
sandbox> exited with status 0
//注:const 的隱式轉(zhuǎn)換:
#include <iostream>
using namespace std;
int main() {
double b = 2.14;
const int &a = b;
// 會(huì)進(jìn)行如下轉(zhuǎn)換:
// int temp = b;
// const int &a=temp;
// 所以,給b進(jìn)行賦值,a可能
cout<<"a="<<a<<endl;
return 0;
}
運(yùn)行結(jié)果:
a=2
sandbox> exited with status 0
//注:修飾成員函數(shù)_1:
class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
int GetDay(void) const
{
m_day=7;
return m_day;//修飾的情況下,不能對(duì)成員變量進(jìn)行修改;
}
};
// void GetDay(void) const
// {
// return m_day;
// }
int main() {
double b = 2.14;
const int &a = b;
// 會(huì)進(jìn)行如下轉(zhuǎn)換:
// int temp = b;
// const int &a=temp;
// 所以,給b進(jìn)行賦值,a可能
cout<<"a="<<a<<endl;
return 0;
}
錯(cuò)誤提示:
/code/main.cpp: In member function ‘int Date::GetDay() const':
/code/main.cpp:16:8: error: assignment of member ‘Date::m_day' in read-only object
m_day=7;
^
sandbox> exited with status 0
//注:修飾函數(shù)_2
#include <iostream>
using namespace std;
class Date
{
private:
int m_year;
int m_month;
mutable int m_day;//通過(guò)被mutable修改的成員變量,就可以被修改了
public:
int GetDay(void) const
{
m_day=7;
return m_day;
}
};
// void GetDay(void) const
// {
// return m_day;
// }
int main() {
double b = 2.14;
const int &a = b;
// 會(huì)進(jìn)行如下轉(zhuǎn)換:
// int temp = b;
// const int &a=temp;
// 所以,給b進(jìn)行賦值,a可能
cout<<"a="<<a<<endl;
return 0;
}
運(yùn)行結(jié)果:
a=2
sandbox> exited with status 0
//注:const修飾的指針
#include <iostream>
using namespace std;
int main() {
const int* p = NULL;//這兩種修飾的是*p指向的值
//int const* p = NULL;
int a=9;
p=&a;//修改了p指向的地址,任然沒有出錯(cuò)
cout<<"*p="<<*p<<endl<<"p="<<p<<endl;
int c=10;
int* const b = &c;//這兩種修飾的是p指向的地址
c=45;
*b=c;//修改了b指向的值,任然不會(huì)出錯(cuò)
cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
b=&a;//這里有問(wèn)題了,b指向的地址是不能修改的
cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
return 0;
}
運(yùn)行結(jié)果:
/code/main.cpp: In function ‘int main()':
/code/main.cpp:21:3: error: assignment of read-only variable ‘b'
b=&a;
^
sandbox> exited with status 0
//注:const修飾的引用
#include <iostream>
using namespace std;
int main() {
int x = 3;
const int& y = x;
cout<<"y="<<y<<endl;
x=9;
cout<<"y="<<y<<endl;
y=9;//const修飾的引用是不能夠在更改引用指向的對(duì)象的
cout<<"y="<<y<<endl;
return 0;
}
運(yùn)行結(jié)果:
/code/main.cpp: In function ‘int main()':
/code/main.cpp:13:3: error: assignment of read-only reference ‘y'
y=9;
^
sandbox> exited with status 0
到此這篇關(guān)于C++中const的特性的使用的文章就介紹到這了,更多相關(guān)C++ const的特性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c語(yǔ)言中十進(jìn)制轉(zhuǎn)二進(jìn)制顯示小工具的實(shí)現(xiàn)代碼
本篇文章是對(duì)c語(yǔ)言中十進(jìn)制轉(zhuǎn)二進(jìn)制顯示小工具的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析的介紹,需要的朋友參考下2013-05-05
基于C++詳解數(shù)據(jù)結(jié)構(gòu)(附帶例題)
數(shù)據(jù)結(jié)構(gòu)作為每一個(gè)IT人不可回避的問(wèn)題,本文基于C++編寫,下面這篇文章主要給大家介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
C語(yǔ)言計(jì)算器的3種實(shí)現(xiàn)方法代碼
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言計(jì)算器的3種實(shí)現(xiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一的參考借鑒價(jià)值,需要的朋友可以參考下2007-01-01
C語(yǔ)言中sizeof 和 strlen的區(qū)別
sizeof?和?strlen?是兩個(gè)常用于 C/C++ 語(yǔ)言中的函數(shù)或操作符,本文主要介紹了C語(yǔ)言中sizeof 和 strlen的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
C++編寫LINUX守護(hù)進(jìn)程的實(shí)現(xiàn)代碼
這篇文章主要介紹了如何使用C++實(shí)現(xiàn)LINUX守護(hù)進(jìn)程,文中代碼非常詳細(xì),供大家學(xué)習(xí)參考,感興趣的小伙伴可以了解下2020-06-06

