const。指针和引用?

问题描述:

常量指针和引用之间有什么区别?

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?

cheers

有三种类型的常量指针:

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 it's 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.

请参阅我的回答这里是引用和指针之间的更多差异