简体   繁体   English

在我们使用指针之前,为什么对数组的引用不起作用?

[英]Why doesn't a reference to an array work until we use a pointer?

This works very well... 这非常有效......

int a[5] = {1,2,3,4,5}, int *p = a;
int *& ref = p;

But why doesn't this work? 但为什么这不起作用?

int a[5] = {1,2,3,4,5};
int*& ref = a;

Both a and p are pointers and have the same value (address of a[0] ). ap都是指针并且具有相同的值( a[0]地址)。 When I make reference to an array using a pointer ( p ), it works very well. 当我使用指针( p )引用数组时,它工作得很好。

But when I make reference to that array a[] directly, it doesn't work... Why? 但是当我直接引用那个数组a[] ,它不起作用......为什么?

a is not a pointer, it is an array. a不是指针,而是数组。 It has the type int[5] . 它的类型为int[5] What it can do is decay to a pointer int* , which is what happens in the first case. 它可以做的是衰减到指针int* ,这是在第一种情况下发生的事情。 So, taking a reference to p is ok. 所以,参考p是可以的。

Now for the second case. 现在是第二种情况。 Remember that a is not a pointer. 请记住, a 不是指针。 So, there is an implicit conversion happening from int[5] to int* . 因此,从int[5]int*发生了隐式转换。 The result of that conversion is a prvalue. 转换的结果是prvalue。 But you can't bind a non-const lvalue reference (which is what ref is) to an rvalue! 但是你不能将一个非const左值引用(这是ref是什么)绑定到右值! So the code fails to compile. 所以代码无法编译。

Here's an analogy: 这是一个类比:

double a = 1.4;
int& b = a; // implicit conversion from 'double' to `int` results in prvalue
            // and you can't bind non-const lvalue refs to rvalues.

Adding on to what has already been answered, you can get a reference to an array like 添加已经回答的内容,您可以获得对数组的引用

int a[5];
int (&ref)[5] = a;

Live 生活

int*& ref = a;

int* is a pointer type, not an array type. int*是指针类型,而不是数组类型。 So, that's why it won't bind to a , which has type int[5] . 所以,这就是为什么它不会绑定到具有int[5]类型的a

So, use const 所以,使用const

int* const& ref = a;

It's works fine, Because the array name is an address constant, non-const reference can not refer to a constant. 它工作正常,因为数组名称是地址常量,非const引用不能引用常量。

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

相关问题 为什么将指针转换为引用在我的访问器(getter)中不起作用? - Why converting a pointer into a reference doesn't work in my accessor (getter)? 为什么不使用引用地址指针编译 C++ ? - Why doesn't make work on a C++ compile with a reference address pointer? 为什么NULL指针不起作用? - Why doesn't NULL pointer work? 为什么我们必须在复制构造函数的参数中使用引用而不是指针? - Why we have to use a reference in argument of copy constructor instead of a pointer? 为什么此模板规范不起作用以供参考? - Why this template specification doesn't work for reference? 为什么在这种情况下转发参考无效? - Why doesn't forwarding reference work in this case? 当参数是指向数组的解引用指针时,为什么 `sizeof()` 不能按预期工作? - Why doesn't `sizeof()` work as expected when the argument is a dereferenced pointer pointing to an array? 为什么更改指针的引用不会改变它的原始值? - Why changing the reference of a pointer doesn't change it's original value? 如果在模板函数中通过const引用传递,数组不会衰减到指针 - array doesn't decay to pointer if passed by const reference in a template function 为什么我们不应该使用在类中返回数组或指针的函数 - Why should not we use a function that returns an array or pointer in a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM