C++程序员如何向一个java工程师解释何为reference引用?
2016-03-03 21:57
567 查看
继昨天某Android程序员问我extern “c”的作用后,她今天又问我什么是引用?
我回答他,引用就是别名,我引用你,我改变了,你也跟着改变。
那就再写个博客吧!
可能没学过C++的人更多的疑问就是,有了指针为何又要有引用呢?二者的区别何在?
A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
指针可以被重新赋值,而引用不可以
Pointers can point nowhere (NULL), whereas reference always refer to an object.
指针可以指向空,引用不能为空
You can’t take the address of a reference like you can with pointers.
你不能获得一个引用的地址
A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack vs heap. This implies that there is a real address of a reference that the compiler will not tell you.
You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
可以指针指向指针,不能引用引用引用。(绕口令,但我相信你能明白)
Const references can be bound to temporaries. Pointers cannot (not without some indirection):
Use references in function parameters and return types to define useful and self-documenting interfaces.
函数的参数最好使用引用,如果返回值有意义,最好也返回引用。但是不要返回局部变量的引用
Use pointers to implement algorithms and data structures
在算法和数据结构中最好使用指针
我回答他,引用就是别名,我引用你,我改变了,你也跟着改变。
那就再写个博客吧!
可能没学过C++的人更多的疑问就是,有了指针为何又要有引用呢?二者的区别何在?
A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
指针可以被重新赋值,而引用不可以
int x = 5; int y = 6; int *p; p = &x; p = &y; *p = 10; assert(x == 5); assert(y == 10); int x = 5; int y = 6; int &r = x;
Pointers can point nowhere (NULL), whereas reference always refer to an object.
指针可以指向空,引用不能为空
You can’t take the address of a reference like you can with pointers.
你不能获得一个引用的地址
A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack vs heap. This implies that there is a real address of a reference that the compiler will not tell you.
int x = 0; int &r = x; int *p = &x; int *p2 = &r; assert(p == p2);
You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
可以指针指向指针,不能引用引用引用。(绕口令,但我相信你能明白)
int x = 0; int y = 0; int *p = &x; int *q = &y; int **pp = &p; pp = &q;//*pp = q **pp = 4; assert(y == 4); assert(x == 0);
Const references can be bound to temporaries. Pointers cannot (not without some indirection):
const int &x = int(12); //legal C++ int *y = &int(12); //illegal to dereference a temporary
Use references in function parameters and return types to define useful and self-documenting interfaces.
函数的参数最好使用引用,如果返回值有意义,最好也返回引用。但是不要返回局部变量的引用
Use pointers to implement algorithms and data structures
在算法和数据结构中最好使用指针
相关文章推荐
- Problem D、F: C语言习题 矩阵元素变换
- C++程序员如何向一个java工程师解释何为reference引用?
- c++ 函数的隐藏和覆盖
- Problem C: C语言习题 整数排序
- C++类的声明与实现中容易忽视的重定义默认参数的错误
- c++ 基类已经存在virtual修饰的同名成员函数,派生类加不加无所谓,都是virtual的
- 【C/C++学院】0907-象棋五子棋代码分析/寻找算法以及排序算法
- 奇怪的c语言
- C语言SOCKET编程指南
- 《Effective C++ 》学习笔记——条款11
- 【C++学习之路】派生类的构造函数(二)
- C++的初始化与清除(补充1)
- DSP 2812: 使用C++封装定时器及应用举例
- 【C++】 单链表 .cpp
- 1095. Cars on Campus (30)
- DSP 2812: 使用C++封装中断控制器PIE
- C指针作为函数参数引用
- c++中strtok的用法,杭电2072
- string类(c++)
- C语言_函数指针