一、引用与指针赋值操作的差别
C++ primer中的说明:
考虑以下两个程序段。第一个程序段将一个指针赋给另一指针:
int ival = 1024, ival2 = 2048; int *pi = &ival, *pi2 = &ival2; pi = pi2; // pi now points to ival2
After the assignment, ival, the object addressed by pi remains unchanged. The assignment changes the value of pi, making it point to a different object. Now consider a similar program that assigns two references:
赋值结束后,pi 所指向的 ival 对象值保持不变,赋值操作修改了 pi 指针的值,使其指向另一个不同的对象。现在考虑另一段相似的程序,使用两个引用赋值:
int &ri = ival, &ri2 = ival2; ri = ri2; // assigns ival2 to ival
This assignment changes ival, the value referenced byri, and not the reference itself. After the assignment, the two references still refer to their original objects, and the value of those objects is now the same as well.
这个赋值操作修改了 ri 引用的值 ival 对象,而并非引用本身。赋值后,这两个引用还是分别指向原来关联的对象,此时这两个对象的值相等。
1、引用仅是别名,不分配内存;
2、可以声明指针的引用:
int *a;
int * &p=a;
二、函数使用引用返回
1、不能是不存在的值,如函数局部变量。
2、常用于返回对象的引用
Q for C++:
1、main与被调函数的参数传递的栈内存分布,函数值副本,const引用返回,const 函数定义体
2、对象模型(对象内存布局):虚函数表,虚基指针,this调用
3、const与&,指针,静态成员
4、private 构造函数与析构函数屏蔽默认函数,const类成员(流对象)
5、位运算,float内存布局,类型转换
6、转换构造函数与重载转换运算符,拷贝构造函数与重载赋值运算符
7、ostream(继承类的应用),try,throw,catch
8、树,图结构与算法,排序算法
9、new与delete,堆分配的检查,delete后指针置0;数组越界的检查
。。。
如:
const变量存储在全局数据区?
const int *p与int * const p与const int *const p的使用
const参数的使用与const返回值的使用