1、const int i = 5 在编译时编译器如define方法处理,若需要作用于其他文件应加extern 改为externconst int
2、reference to const : const int &rtoc= const int odata
int i = 40;
const int &r1 = i; //允许将const int&绑定在一个普通int
const int &r2 = 40 * 2;//同样合法,40字面值为int
int &r3 = r1; //错误
3、常量引用可能引用一个非常量的值(参数列表)
4、pointer to const
const double pi = 3.14D;
double p* = π //Wrong!
const double *cp = π //Right
double dval = 3.14;
cp = &dval; //Right
5、const pointer
int i = 10;
int *const curi = &i;
const double pi = 3.14159;
const double *const pip = π//constpointer to const常量常指针
6、top-level const and low-level const
top-level const : object is a const(class orobject or pointer);
low-level const : pointer or reference tosth;
执行拷贝或赋值操作时须有相同的底层const资格
int i = 0;
int *const p1 = &i; //top
const int ci = 40; //top
const int *p2 = &ci; //low
7、指针、常量和类型别名
typedef char *pstring;
const pstring cstr = 0; // const pointer tochar
const pstring *ps; // pointer to const pointer to char
其中typedef不能理解为填充替换含义,pstring 的基本数据类型是指针 ,而 char* 的基本数据类型为char
const pstring cstr = 0;//指向char的const pointer
const char *cstr = 0; //指向const char的pointer