开发者

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

开发者 https://www.devze.com 2022-12-21 13:44 出处:网络
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.

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 a开发者_如何学Cnybody 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:

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. 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:

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). Consider the following totally legal Java example using an array of references:

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.

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. 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:

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. In fact, Java references can have a null value just like pointers can have nullptr value.
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:

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

精彩评论

暂无评论...
验证码 换一张
取 消