C++中const的特性
目錄(作用):
1:修飾變數,說明該變數不可以被改變;
2:修飾指針,分為只想常量的指針和自身是常量的指針
3:修飾引用,指向常量的引用,用於修飾形參,即避免了拷貝,有避免了函數對值的修改;
4:修改成員函數:說明該成員函數內不能修改成員變數。
5:指針與引用
正文:
以下是對各種情況的示例:
註:1:const修飾的引用cj的值且引用的對象無法修改無法修改,但是引用的i是可修改的 1 #include <iostream> 2 3 using namespace std; 4 5 int main() { 6 int i = 1; 7 const int &cj = i; 8 9 cout << "cj : " <<cj<< endl;(√) 10 11 12 i=2; 13 cout << "cj : " <<cj<< endl;(√) 14 15 cj=3; 16 cout << "cj : " <<cj<< endl;(×) 17 18 int a=9; 19 cj=a; (×) 20 21 return 0; 22 } 23 24 25 26 錯誤提示: 27 /code/main.cpp: In function 『int main()』: 28 /code/main.cpp:15:4: error: assignment of read-only reference 『cj』 29 cj=3; 30 ^ 31 /code/main.cpp:19:4: error: assignment of read-only reference 『cj』 32 cj=a; 33 ^ 34 sandbox> exited with status 0 35
1 註:常量引用,本身也要是常量才行: 2 3 #include <iostream> 4 5 using namespace std; 6 7 int main() { 8 const int i = 4; 9 10 const int &ck = i; //正確,常量對象綁定到 const引用 11 cout<< "ck="<<ck<<endl; 12 13 const int b = 5; 14 15 int &r = b; //錯誤, 16 17 return 0; 18 } 19 20 21 22 /code/main.cpp: In function 『int main()』: 23 /code/main.cpp:13:14: error: invalid initialization of reference of type 『int&』 from expression of type 『const int』 24 int &r = b; //錯誤, 25 ^ 26 sandbox> exited with status 0
1 註:const 的隱式轉換: 2 3 #include <iostream> 4 5 using namespace std; 6 7 int main() { 8 double b = 2.14; 9 const int &a = b; 10 // 會進行如下轉換: 11 // int temp = b; 12 // const int &a=temp; 13 // 所以,給b進行賦值,a可能 14 cout<<"a="<<a<<endl; 15 return 0; 16 } 17 18 運行結果: 19 a=2 20 sandbox> exited with status 0
1 註:修飾成員函數_1: 2 3 class Date 4 { 5 private: 6 int m_year; 7 int m_month; 8 int m_day; 9 public: 10 int GetDay(void) const 11 { 12 m_day=7; 13 return m_day;//修飾的情況下,不能對成員變數進行修改; 14 } 15 }; 16 17 // void GetDay(void) const 18 // { 19 // return m_day; 20 21 // } 22 23 int main() { 24 double b = 2.14; 25 const int &a = b; 26 // 會進行如下轉換: 27 // int temp = b; 28 // const int &a=temp; 29 // 所以,給b進行賦值,a可能 30 cout<<"a="<<a<<endl; 31 return 0; 32 } 33 34 35 錯誤提示: 36 /code/main.cpp: In member function 『int Date::GetDay() const』: 37 /code/main.cpp:16:8: error: assignment of member 『Date::m_day』 in read-only object 38 m_day=7; 39 ^ 40 sandbox> exited with status 0
1 註:修飾函數_2 2 3 #include <iostream> 4 5 using namespace std; 6 7 8 9 class Date 10 { 11 private: 12 int m_year; 13 int m_month; 14 mutable int m_day;//通過被mutable修改的成員變數,就可以被修改了 15 public: 16 int GetDay(void) const 17 { 18 m_day=7; 19 return m_day; 20 } 21 }; 22 23 // void GetDay(void) const 24 // { 25 // return m_day; 26 27 // } 28 29 int main() { 30 double b = 2.14; 31 const int &a = b; 32 // 會進行如下轉換: 33 // int temp = b; 34 // const int &a=temp; 35 // 所以,給b進行賦值,a可能 36 cout<<"a="<<a<<endl; 37 return 0; 38 } 39 40 41 運行結果: 42 a=2 43 sandbox> exited with status 0
1 註:const修飾的指針 2 3 4 #include <iostream> 5 6 using namespace std; 7 8 9 int main() { 10 const int* p = NULL;//這兩種修飾的是*p指向的值 11 //int const* p = NULL; 12 13 int a=9; 14 p=&a;//修改了p指向的地址,任然沒有出錯 15 cout<<"*p="<<*p<<endl<<"p="<<p<<endl; 16 17 18 int c=10; 19 int* const b = &c;//這兩種修飾的是p指向的地址 20 c=45; 21 *b=c;//修改了b指向的值,任然不會出錯 22 cout<<"*b="<<*b<<endl<<"b="<<b<<endl; 23 24 b=&a;//這裡有問題了,b指向的地址是不能修改的 25 cout<<"*b="<<*b<<endl<<"b="<<b<<endl; 26 return 0; 27 } 28 29 運行結果: 30 /code/main.cpp: In function 『int main()』: 31 /code/main.cpp:21:3: error: assignment of read-only variable 『b』 32 b=&a; 33 ^ 34 sandbox> exited with status 0
1 註:const修飾的引用 2 3 #include <iostream> 4 5 using namespace std; 6 7 8 int main() { 9 int x = 3; 10 const int& y = x; 11 cout<<"y="<<y<<endl; 12 x=9; 13 cout<<"y="<<y<<endl; 14 15 y=9;//const修飾的引用是不能夠在更改引用指向的對象的 16 cout<<"y="<<y<<endl; 17 return 0; 18 } 19 20 21 運行結果: 22 /code/main.cpp: In function 『int main()』: 23 /code/main.cpp:13:3: error: assignment of read-only reference 『y』 24 y=9; 25 ^ 26 sandbox> exited with status 0