C++ 对指针的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3128662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:07:44  来源:igfitidea点击:

Reference to a pointer

c++referencepointers

提问by Sam

char *str = "Hello";

char *ptr = str;
char *&rptr = str;

What is the difference between ptr and rptr? I understand rptr is a reference to a pointer(in theory) but how does it differ in terms of implementation with ptr?

ptr 和 rptr 有什么区别?我理解 rptr 是对指针的引用(理论上),但它在使用 ptr 的实现方面有何不同?

Are references in C++ implemented using pointers?

C++ 中的引用是使用指针实现的吗?

回答by sepp2k

What is the difference between ptr and rptr?

ptr 和 rptr 有什么区别?

If you do char *world = "World"; rptr = world;and then print str, it will print "World". If you do ptr = world;and then print str, it will print "Hello".

如果你这样做char *world = "World"; rptr = world;然后打印str,它会打印“世界”。如果你这样做ptr = world;然后打印str,它会打印“你好”。

回答by stinky472

strstores the address (and therefore points) to a literal string, "Hello", which is stored somewhere in a read-only segment of memory.

str将地址(并因此指向)存储到文字字符串“Hello”,该字符串存储在只读内存段的某处。

ptrpoints to the same address as 'str'.

ptr指向与 'str' 相同的地址。

rptrbasically points to 'str' (not to the same pointee as str, but to str itself). It might be unusual to use 'points to' for references but they're really very much like pointers themselves (in this case a pointer to a pointer) except with slight syntactical differences and the restriction that they cannot point to any other address during their lifetime.

rptr基本上指向 'str'(不是指向与 str 相同的指针,而是指向 str 本身)。使用“指向”作为引用可能很不寻常,但它们实际上非常像指针本身(在这种情况下是指向指针的指针),只是有轻微的语法差异和在它们在执行期间不能指向任何其他地址的限制寿命。

It would be analogous to:

这将类似于:

char** const rptr = &str;

Like a reference, rptr above cannot be assigned a new address (it cannot change what it's pointing to), but it can be free to change its pointee (which happens to be a pointer in this case to 'str').

像引用一样,上面的 rptr 不能被分配一个新地址(它不能改变它指向的东西),但它可以自由地改变它的指针(在这种情况下恰好是一个指向“str”的指针)。

*rptr = 0; // after this, str == 0

References are pretty much the same as a read-only pointer (not a mutable pointer to read-only pointee) only they don't require a dereferencing operator to get at the pointee (the referenced data):

引用与只读指针(不是指向只读指针的可变指针)几乎相同,只是它们不需要取消引用运算符来获取指针(引用的数据):

char *str = "Hello";
char *&rptr = str;
rptr = 0; // after this, str == 0

The only difference from the above example with a read-only pointer to pointer is that we didn't have to use the operator*.

与上面带有只读指针的示例的唯一区别是我们不必使用运算符*。

const references also have the unique property of being able to extend the lifetime of temporaries, but that's probably beyond the scope of the discussion.

const 引用还具有能够延长临时对象生命周期的独特属性,但这可能超出了讨论的范围。

回答by Kleist

Try this:

尝试这个:

#include <iostream>
int main () {
    char *str1 = "Hello ";
    char *str2 = "World!";
    char *ptr = str1;
    char *&rptr = str1;
    rptr = str2;
    std::cout << ptr << str1 << std::endl;
}

It prints "Hello world!" because ptris a char pointer pointing to the string literal "Hello ". rptris a reference to a char pointer, so when you change it (the pointer, not the thing it points at.) you change str1. And thus ptrpoints to "Hello ". str1points to "World!".

它打印“你好世界!” 因为ptr是指向字符串文字“Hello”的字符指针。rptr是对 char 指针的引用,因此当您更改它(指针,而不是它指向的对象)时,您会更改str1. 从而ptr指向“你好”。str1指向“世界!”。