Difference between const. pointer and reference?(常量之间的区别.指针和引用?)
问题描述
常量指针和引用有什么区别?
What is the difference between a constant pointer and a reference?
常量指针顾名思义是不能再绑定的.参考文献也是如此.
Constant pointer as the name implies can not be bound again. Same is the case with the reference.
我想知道在哪种情况下,一种会比另一种更受欢迎.他们的 C++ 标准和实现有何不同?
I wonder in what sort of scenarios would one be preferred over the other. How different is their C++ standard and their implementations?
干杯
推荐答案
const 指针有 3 种类型:
There are 3 types of const pointers:
//Data that p points to cannot be changed from p
const char* p = szBuffer;
//p cannot point to something different.
char* const p = szBuffer;
//Both of the above restrictions apply on p
const char* const p = szBuffer;
上面的方法#2 与参考文献最相似.
Method #2 above is most similar to a reference.
引用和上述所有 3 种类型的 const 指针之间存在主要区别:
There are key differences between references and all of the 3 types of const pointers above:
常量指针可以为 NULL.
Const pointers can be NULL.
引用没有自己的地址,而指针有.
引用的地址是实际对象的地址.
A reference does not have its own address whereas a pointer does.
The address of a reference is the actual object's address.
一个指针有它自己的地址,并且它的值是它指向的值的地址.
A pointer has its own address and it holds as its value the address of the value it points to.
在此处查看 我的回答参考和指针之间的更多区别.
这篇关于常量之间的区别.指针和引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:常量之间的区别.指针和引用?


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01