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.
在此处查看 我的回答参考和指针之间的更多区别.
这篇关于常量之间的区别.指针和引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:常量之间的区别.指针和引用?


- 静态初始化顺序失败 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- C++ 协变模板 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01