简体   繁体   English

C++:为什么我们不能有对引用或引用数组的引用?

[英]C++: why can't we have references to references or array of references?

I noticed that there is no reference to reference but there is pointer to pointer, and also there is no an array of references but an array of pointers.我注意到没有对引用的引用但是有指向指针的指针,也没有引用数组而是指针数组。

Could anybody give me any reason?谁能给我任何理由?

Pointers are mutable (if non-const), references never. 指针是可变的(如果是非常量的),引用永远不会。 Thus there is no point having a pointer or reference to a reference. 因此,没有必要指向引用的指针或引用。

Also, a reference must always refer to something - there is no such thing as a null reference. 此外,引用必须始终引用某些东西 - 没有空引用这样的东西。 This is why there can be no arrays of references, as there is no way to default instantiate references inside an array to a meaningful value. 这就是为什么没有引用数组的原因,因为没有办法将数组内的引用实例化为有意义的值。

It is according to C++ Standard 8.3.2/4: 它符合C ++标准8.3.2 / 4:

There shall be no references to references, no arrays of references, and no pointers to references. 不应引用引用,不引用引用数组,也不引用引用指针。

A reference is an abstraction at the language level. 引用是语言级别的抽象。 It's an opaque way of aliasing a variable to another. 这是将变量别名化为另一个变量的不透明方式。 While under the hood, the compiler is likely to work out references using pointers, they are very different things at a higher level. 虽然在幕后,编译器可能会使用指针来计算引用,但它们在更高级别上是非常不同的东西。 On the other hand, pointers are explicitly used by a programmer to achieve indirection. 另一方面,程序员明确地使用指针来实现间接。 A pointer variable is a distinct variable from what it's pointed to. 指针变量是与其指向的不同的变量。 A reference should be thought as if it's simply an alias to the original variable, not as if it's yet another variable holding an address. 引用应该被认为好像它只是原始变量的别名,而不是它是另一个持有地址的变量。 Consequently, an alias for an alias of a variable would simply be an alias to the variable itself. 因此,变量别名的别名只是变量本身的别名。 Considering the binding a reference to a variable is a compile-time thing may help understanding the rationale behind this behavior. 考虑到绑定,对变量的引用是编译时的事情可能有助于理解这种行为背后的基本原理。

With this reasoning, you can argue that since arrays are structures that store values, not variables, it doesn't make sense for them to be able to store aliases of variables. 有了这个推理,你可以争辩说,因为数组是存储值而不是变量的结构,所以它们能够存储变量的别名是没有意义的。 Basically, a reference variable (by which I mean the pointer, if exists, that may be used by the compiler to wire up the reference) is invisible to the programmer at the C++ level. 基本上,一个引用变量(我的意思是指针,如果存在, 可以由编译器用来连接引用)对程序员来说是C ++级别不可见的。 If it was possible to declare arrays of references, the compiler probably needed to require constant indexes passed to the arrays to be able to resolve the binding at compile time. 如果可以声明引用数组,则编译器可能需要传递给数组的常量索引才能在编译时解析绑定。

C++ Standard 8.3.2/4: C ++标准8.3.2 / 4:

There shall be no references to references, no arrays of references, and no pointers to references.

The reasoning behind this is that a reference doesn't exist in and of itself at run-time. 这背后的原因是在运行时不存在引用本身。 A reference merely is another name for a location. 引用仅仅是位置的另一个名称。 They are immutable. 他们是不变的。

I think we probably need to start by reiterating that arrays of references are used all the time in Java and Python. In fact, a Java programmer never really tries to directly use addresses of data (pointers).我认为我们可能需要首先重申 arrays 的引用一直在 Java 和 Python 中使用。事实上,Java 程序员从未真正尝试直接使用addresses of data (指针)。 Consider the following totally legal Java example using an array of references:考虑以下使用引用数组的完全合法的 Java 示例:

String[] array_of_references = new String[2]; // create array of String object references
array_of_references[0] = "Hello"; // "Hello" is a string object reference
array_of_references[1] = "World!";
print(array_of_references[0].toLowerCase()); // use dot to access method of ref.
// prints hello

I am lying a little here because references in C++ are not exactly the same as references in Java.我在这里有点撒谎,因为 C++ 中的引用与 Java 中的引用不完全相同

The difference that makes it impossible to create an array of references in C++ is the restriction that every reference in C++ has to *always*(in all scopes) be assiciated with some variable *name* in the program .无法在 C++ 中创建引用数组的区别是 C++ 中的every reference in C++ has to *always*(in all scopes) be assiciated with some variable *name* in the program In other words, to create an array of references in C++, you are required to manually name every element of the array, which goes against the usefulness of arrays. Here is my (brave yet pitiful) attempt to implement a C++ array-of-references of fixed size 3:换句话说,要在 C++ 中创建引用数组,您需要手动命名数组的每个元素,这与 arrays 的用处背道而驰。这是我(勇敢但可怜)尝试实现 C++ 数组的-固定尺寸 3 的参考资料:

class ArrayOfReferences_size3{
  public:
    // constructor
    ArrayOfReferences_size3(int &el_1, int &el_2, int &el_3):
    first_el(el_1), second_el(el_2), third_el(el_3) {}

    int& first_el; // gotta name it again
    int& second_el; // gotta name it again
    int& third_el; // gotta name it again
};

int main() {
  int el_1 = 1; // gotta name the value
  int el_2 = 2; // gotta name the value
  int el_3 = 3; // gotta name the value
  ArrayOfReferences_size3 my_array(el_1, el_2, el_3);
  cout << my_array.first_el 
       << my_array.second_el 
       << my_array.third_el << "\n";
}

Java can allow references to be unnamed because java references can represent thier own value like c++ pointers can -- they don't need to be bound to a name. Java 可以允许引用未命名,因为 java 引用可以像 c++ 指针一样表示它们自己的值——它们不需要绑定到名称。 In fact, Java references can have a null value just like pointers can have nullptr value.事实上,Java 引用可以有一个null值,就像指针可以有nullptr值一样。
In the example below, notice how the String object reference "Hello" is unnamed and the only way to reach it is by indexing into the array:在下面的示例中,请注意字符串 object 引用“Hello”是如何未命名的,并且访问它的唯一方法是通过索引到数组中:

array_of_reference[0] = "Hello"; // Impossible with C++ references

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM