总结介绍C++标准中Const的用法.Code & Algorithm.
1.用于定义常量,防止被修改。
const int val = 10;
val = 20;//错误.
2.保护传参时,参数不被修改。
当使用引用传递参数时或按地址传递参数给一个函数时,在这个函数里这个参数的值若被修改,则函数外部传进来的变量的值也发生改变。若想保护传进来的变量不被修改,可以使用const保护.
例如:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | 
						 void  fun1(const int &val)   {      //val = 10; //出错 } void fun2(int &val) {    val = 10; //没有出错 } void main() {    int a = 2;    int b = 2;    fun1(a); //因为出错,这个函数结束时a的值还是2    fun2(b);//因为没有出错,函数结束时b的值为10 }  | 
					
如果只想传递值而且不可以修改这个值,则可以使用const保护变量.
为什么不使用传值:按值传递还需要把这个变量复制一遍,而引用不需要,使用引用是为了提高效率。
如果使用按值传递,则不需要加const,因为没意义。
3.可以节约内存空间
定义常量可以使用宏定义#define 和 const 两种方式.
例如:
| 
					 1 2 3 4 5 6 7  | 
						#define  PI  3.14 //使用#define宏 const double Pi = 3.14 //使用const,这时候Pi并没有放入内存中 double  a = Pi;  //这时候才为Pi分配内存,不过后面再有这样的定义也不会再分配内存 double  b = PI;  //编译时分配内存 double  c = Pi;  //不会再分配内存, double  d = PI;  //编译时再分配内存  | 
					
const定义的变量,系统只为它分配一次内存,而使用#define定义的常量宏,会多次分配内存,这样使用const就很节约空间了。
4.类中使用const修饰函数防止修改非static类成员变量.
即:如果我们使用const修饰某个函数,则在该函数内不能修改非static类成员变量.
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						class { public:     void fun() const //加const修饰     {         a = 10; //出错,不可修改非static变量         b = 10; //对,可以修改     } private:     int  a ;     static int b; }  | 
					
5.修饰指针.
修饰指针有三种情况:
- const int* A; 或者 int const * A;//const修饰指向的对象,指向的对象可变,但指向的对象的值不可变.
 - int* const A;//const 修饰 指针A ,指向的对象不可变,指向的对象的值可变.
 - const int* const A;//指针A指向的对象和那个对象的值,都不可变.
 
情况1:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | 
						#include<iostream> using namespace std; int main() { 	int a = 3; 	int b = 4; 	const int* A = &a; 	cout << *A << endl; 	A = &b;//指针A指向的对象可变 	cout << *A << endl; 	//指向的对象的值不可变 	//*A = 4;//报错:[Error] assignment of read-only location '* A' 	cout << *A << endl; }  | 
					
情况2:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | 
						#include<iostream> using namespace std; int main() { 	int a = 3; 	int b = 4; 	int* const A = &a; 	cout << *A << endl; 	*A += 1;//指向的对象的值可变 	cout << *A << endl; 	//指向的对象不可变 	//A = &b;//[Error] assignment of read-only variable 'A' 	cout << *A << endl; }  | 
					
情况3:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | 
						#include<iostream> using namespace std; int main() { 	int a = 3; 	int b = 4; 	const int* const A = &a; 	cout << *A << endl; 	//指向的对象和指向的对象的值都不可变 	//*A += 1;//[Error] assignment of read-only location '*(const int*)A' 	cout << *A << endl; 	//A = &b;//[Error] assignment of read-only variable 'A' 	cout << *A << endl; }  | 
					
6.修饰函数的返回值,防止返回值被改变.
const int fun();
接收返回值的变量也必须加const
const int a = fun(); //接收的变量也要是const的,int a = fun()是错误的
7.修饰类的成员变量
例如://这段代码在C++11标准可用,C++标准会报错.
| 
					 1 2 3 4  | 
						class Const{ const int a = 10; static const int b = 10; }  | 
					
如果在C++标准下。应该是这样:
初始化const int类型(没有static),在类的构造函数上初始化
| 
					 1 2 3 4 5 6 7 8  | 
						Class Test { Public:     Test():b(23){} //构造函数上初始化b的值为23 private:     const int b ; }  | 
					
初始化static const int这个类型的(带有static的),在类的外面初始化
| 
					 1 2 3 4 5 6  | 
						class Test { private:   static const int c; }  const int Test::c=10; //类的外部初始化c为10  | 
					
8.const定义的对象变量在别的文件使用时,也必须是const变量.
如file1.cpp中 const int val;
在file2.cpp中, extern int val; //错误,无法调用,
要想const定义的对象变量能被其他文件调用,定义时必须使用extern修饰为 extern const int val;
非const变量默认为extern,要是const能被其他文件访问必须显示指定为extern.
实例如下:
1.打开一个项目有main.cpp 和 未命名2.cpp两个文件
在未命名文件中定义 const int val = 1;

接下来在 main 函数中定义 extern int val = 10; (不能使用int val = 10;)
编译发现链接的时候报错,所以加上const

可以发现,编译链接通过了,不再报错了…
大致就是这些…如果还有,会继续补充.
