为什么在 C++ 中指向引用的指针是非法的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4632762/
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 16:00:11  来源:igfitidea点击:

Why are pointers to a reference illegal in C++?

c++pointersreference

提问by Mahesh

As the title itself mentions - why are pointer to a reference illegal, while the reverse is legal in C++?

正如标题本身提到的 - 为什么指向引用的指针是非法的,而在 C++ 中相反是合法的?

Thanks.

谢谢。

回答by James McNellis

A pointer needs to point to an object. A reference is not an object.

指针需要指向一个对象。引用不是对象。

If you have a reference r, once it is initialized, any time you use ryou are actually using the object to which the reference refers.

如果你有一个引用r,一旦它被初始化,任何时候你使用r你实际上是在使用引用所指向的对象。

Because of this, you can't take the address of a reference to be able to get a pointer to it in the first place. Consider the following code:

因此,您无法首先获取引用的地址以获取指向它的指针。考虑以下代码:

int x;
int& rx = x;

int* px = ℞

In the last line, &rxtakes the address of the object referred to by rx, so it's exactly the same as if you had said &x.

在最后一行中,&rx采用 引用的对象的地址rx,因此它与您所说的完全相同&x

回答by AnT

The high-level concept that references implement is just another name for an existing object. You can have a pointer to an object (or function), but you can't have a pointer to an object's name. For this very reason, the idea of a pointer to a reference makes no sense. In other words, references are immaterial, in general case they simply do not exist in memory. They don't exist as something that can be pointed to.

引用实现的高级概念只是现有对象的另一个名称。您可以拥有指向对象(或函数)的指针,但不能拥有指向对象名称的指针。正是出于这个原因,指向引用的指针的想法是没有意义的。换句话说,引用是无关紧要的,通常情况下它们根本不存在于内存中。它们不作为可以指向的东西存在。

It is true that in many cases in practice references do occupy memory (and are implemented as pointers in disguise). But that just an implementation detail specific to some particular contexts. In general case references do not occupy memory, as is explicitly stated in the language specificationwhich immediately follows from the language specification.

确实,在实践中的许多情况下,引用确实会占用内存(并且被伪装成指针)。但这只是特定于某些特定上下文的实现细节。一般情况下引用不占用内存,正如语言规范中明确规定的那样它紧随语言规范。

回答by Potatoswatter

What would be the difference between a pointer to a reference (to the object) and a pointer to the actual object? The reference cannot be changed to refer to another object. Just use a regular pointer to the object in question.

指向引用(对象)的指针和指向实际对象的指针有什么区别?不能将引用更改为引用另一个对象。只需使用指向相关对象的常规指针即可。

On the other hand, a reference to a pointer, like any other reference, gives you a modifiable handle to a particular variable. It happens to be a pointer in this case.

另一方面,对指针的引用,就像任何其他引用一样,为您提供了一个特定变量的可修改句柄。在这种情况下,它恰好是一个指针。

回答by Karl Knechtel

Because a reference is not a thing that can be pointed at, which in turn is because it does not actually have to be represented anywhere in memory. References exist to give alternate names to already-existing things. You can get a pointer to the renamed thing, but that is a pointer to a value, not a pointer to a reference.

因为引用不是可以指向的东西,这反过来是因为它实际上不必在内存中的任何地方表示。存在引用来为已经存在的事物提供替代名称。您可以获得指向重命名的事物的指针,但这是指向值的指针,而不是指向引用的指针。

回答by John

Suppose "int *&X" is legal, because reference is just another name of an object, the expression is equal to "int *X" and is not useful.

假设“int *&X”是合法的,因为引用只是对象的另一个名称,表达式等于“int *X”并且没有用。