const的意思是所修饰的变量不能被修改
const char* s = "woshishabi";
const和指针
const char* s = "woshishabi";
int a = 10;
const int b = 20;
b = a;//err
a = b;//ok
int* p = &a;
const int* p1 = &a;
const int* const p2 = &a;
a = 11;
*p1 = 12;//err
*p2 = 13;
p2 = &b;//err
const int* const* p3 = &p2;//*p3不能改变,即p2不能改变
return 0;
总结:修改权是权利的一种,权利会收缩下放给其他人,但是新来的权利不能比要取代的权利大,要么相等,要么就收缩.
对于指针:constp代表p不能改变,就是所指向的变量的值不能改变
*const p代表p不能改变,就是他的指向一旦确定就不能改变,但是他指向的变量的值却可以改变.以此类推.^^
const和引用
int a = 10;
const int b = 20;
int* p = &a;
const int* p1 = &b;
int* const p3 = &a;
const int& y = b;
int& const y1 = a;
y1 = 11;
总结:本来引用之后就不能改变指向,所以没啥意义.只要对齐权利就行了;
const和引用和指针结合
int a = 10;
const int b = 20;
int* p = &a;
const int* p1 = &b;
const int* const p2 = &b;
int* const p3 = &a;
int*& y1 = p;
const int*& y2 = p1;
int* const& y3 = p3;
const int* const& y4 = p2;
const int c = *y4;
const int* const pp = y4;