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的强大之处。