命名常量
const int SIZE = 3和int const SIZE = 3 相同
其中SIZE叫命名常量,3叫字面常量
今日编码规范(1)
常量名字必须全部大写并用下划线分割单词
常量与指针
指针的两个属性
- 指针变量本身
- 指针变量所指向的数据
指针本身和所指的数据都可以分别为变量或常量
当指针遇上const
const int x = 5;
const int* p = &x;
此时意为:整型指针p只能读取x地址的信息,而不能去对x地址里的信息进行操作
int* const p = &x;
此时意为:p被锁定,只能够指向x的地址,不能再让p指向其他地方
const int* const p = &x;
p既不能修改所指地址的内容,也不能修改所指向的目标地址
常量指针
指针所指向的内容不可以通过指针的间接引用即(*p)来改变
const int x = 1;
const int* p;
p = &x;
*p = 10; 错误!p所指向的是常量,不允许用*p进行修改值
char* s1 = “Hello”; 错误!”Hello”是常量,必须用const对指针进行限制对内容的修改
const char* s2 = “Hello”;
指针常量
指针本身是常量
int x = 0,y = 0;
int* const p = &x;
*p = 10;
p = &y; Error!
数组名就是指针常量
简单粗暴的区分指针常量和常量指针
const在谁前面(最近的),谁就不许变
把*念成指针,const念成常量
const int* p;//常量指针
int* const p = &x;//指针常量
注意:是把*念成指针,不是int
例如:int const * p;//常量指针
using
用using替代typedef(C++11)
using UInt = unsigned int;
//相当于typedef unsigned int UInt;
UInt x = 42u;
using FuncType = void(*)(int,int);
//等同于typedef void(*)(int,int) FuncType; 这是一个指向函数的指针
void example(int,int){}
FuncType f = example;
using只能替换类型
using in = std::cin;//Error!
在定义模板的别名,只能使用using
今日编码规范(2)
代表类型的名字必须首字母大写,并且其他字母大小写混合
例如:SavingAccount