問題描述
常量指針和引用有什么區別?
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.
在此處查看 我的回答參考和指針之間的更多區別.
這篇關于常量之間的區別.指針和引用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!