C ++^1
尽量不要使用指针和数组,因为太过底层,很可能出现不可预测的问题。尽量使用vector等对其的封装的高级用法。
引用 vs 指针
引用 相当于变量的别名
int ivar = 1;
int &i = ivar;
int *pi = &ivar;
cout << "i = "<< i << endl; // 相当于i 为 ivar的别名
cout << "pi = "<< pi << endl;
cout << "*pi = "<< *pi << endl;
cout << "&i = "<< &i << endl;
cout << "&ivar = "<< &ivar << endl;
i = 1
pi = 0x7fff5fbff73c
*pi = 1
&i = 0x7fff5fbff73c
&ivar = 0x7fff5fbff73c
表明指针就是存放对象地址的地方。
constant
const int *p = &ivar;
int a = 4;
p = &a;
int const conPtr = &ivar;
conPtr = 4;
右边的总和不能变
const int * const bothConptr = &ivar;