- A const object must be initialized once it has been created and its value cannot be modified.
- You can initialize a const object with a non-const object, or you can assign a const object to a non-const object
- By default, const objects are only valid within a file
- When a const variable with the same name appears within multiple files, it is equivalent to defining a separate variable in a different file
- To share a const object between multiple files, add the extern keyword before the definition of the variable
1.The & (Reference) type differs from the * (pointer) type,The declaration, definition, and initialization of a reference type are together, and no other variables can be referenced after initialization there is no limit to the declaration, definition, initialization, and subsequent assignment of pointer types.
int a = 1;
int b = 2;
int &c = a;
int *p;
p = &a;
2, with the const collocation
int a = 1;
int b = 2;
/**< 因为引用在被初始化之后就不能在引用其它变量,所以没有int & const c = a这种写法 */
const int &c = a;
/**
* 不能给c赋值,对a无要求(a可以是const或者非const类型,a也可以重新赋值)
* 不能将c赋值给非const类型
*/
const int *p1 = &a;//等同于int const *p1 = &a;
/**
* 底层const
* 不能通过p1修改a的值,对a无要求
* 不能将p1赋值给非const类型
* p1可以指向其他变量
*/
int * const p2 = &a;
/**
* 顶层const
* 可以通过p2修改a的值
* p2不能指向其他变量
*/
const int * const p3 = &a;
If the const is located in*, the const is used to modify the variable pointed to by the pointer, that is, the pointer to a constant;
If the const is located in*on the right, const is the modifier pointer itself, that is, the pointer itself is a constant.
From for notes (Wiz)
C + + Const qualifier