C++ 中的 const 詳解


作者:jostree 轉載請注明出處 http://www.cnblogs.com/jostree/p/4235721.html

1.為什么使用 const int 而不使用 #define

在使用#define時,比如#define Max 10000,如果出現錯誤,編譯器並不會提示Max,因為在預處理階段已經把Max替換成了10000,因此編譯器會莫名其妙的提示10000這個數字出現了錯誤,從而不利於程序debug,但是如果使用const int Max=10000,編譯器就會准確的提示Max有錯誤,從而可以輕易地定位。

 

2.如何正確的使用 const

1)const 修飾變量

對於一個字符指針的定義char * p;加上const關鍵字時無非有三種方法,即把const放在前中后三個位置。那么他們之間有什么區別呢?

  1 const char * p1;//表示p1指向了的字符串不可更改

  2 char const * p2;//同上

  3 char * const p3;//表示p2指針的指向不可更改

注意1和2的意義完全相同,只是不同的程序員具有不同的編碼習慣,我們要熟悉所有的這三種用法,下面給出一段代碼,來說明他們三個的區別:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 #include <limits.h>
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 int main(int argc, char *argv[])
11 {
12     const char * p1 = new char [10]();
13     for( int i = 0 ; i < 10 ; i++ )
14     {
15         cout<<p1[i]<<endl;
16     }
17 
18     p1[0] = 'a';//錯誤!const char * p指向一個不可改變的對象
19     cout<<p1[0]<<endl;
20 
21     char const * p2 = new char[10];
22     p2[0] = 'a';//錯誤!同上
23     cout<<p2[0]<<endl;
24 
25     char ch[5] = "abcd";
26     delete [] p1;
27     p1 = ch;//正確 指針的值可以修改
28     cout<<p1<<endl;
29 
30     char * const p3 = new char[100];
31     p3[0] = 'a';//正確!
32     cout<<p3[0];
33 
34     delete [] p3;
35     p3 = ch;//錯誤 指針的值不可以修改
36     cout<<p3<<endl;
37 }

在第18行和第22行中,由於指針所指向的對象不可修改,因此會出現錯誤,而在35行處其指針的指向不可修改,因此不可把它指向另一個地址。

 

2)const 修飾函數

const不僅可以修飾變量,也可以修飾函數,當const修飾函數,例如[]符重載操作, int & operator [](int n) 時有兩個位置可以放置:

  1 const int & operator[](int n);

  2 int & operator[](int) const;

前者指其返回值不可修改,例如對於一個const對象 cint,我們不可以使用cin[0]=3,這種左值的方式對其進行賦值,因為其是一個const對象,應該不能夠對其內部值進行修改。

而后者的意思是該[]操作符函數內部不對其對象內部進行修改,是一個const函數,如果其函數實現對內部成員進行了修改,則會報錯。

 

對於一個[]操作符,我們往往使用重載的方式來實現其const版本和非const版本,在const版本中,如果對象定義為const其內部成員不可修改,否則可以自由修改。例如如下代碼:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 #include <limits.h>
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 class MyIntArray
11 {
12     private:
13         static const int a = 100;
14         int  p[a];
15     public:
16         MyIntArray()
17         {
18             for( int i = 0 ; i < a ; i++ )
19             {
20                 p[i] = i;
21             }
22         }
23         const int & operator [](int n) const
24         {
25             return p[n];
26         }
27         int & operator [](int n) 
28         {
29             return p[n];
30         }
31 };
32 
33 int main(int argc, char *argv[])
34 {
35     MyIntArray array;//定義一個非const對象
36     cout<<array[20]<<endl;//輸出20
37     array[20] = 6;//正確,調用非const版本的[]
38     cout<<array[20]<<endl;//輸出6
39 
40     const MyIntArray carray;//定義一個const對象
41     cout<<carray[20]<<endl;//輸出20
42     carray[20] = 6;//錯誤!調用const版本的[]操作符
43     cout<<carray[20]<<endl;
44 }

 

對於一個非const對象array我們可以自由的把array[0]作為左值,而對於一個const對象carray我們則不可以把其作為左值。由此可發現const的強大之處。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM