[英]Why doesn't the const lvalue reference to an array compile below?
The const lvalue reference int(const& crb)[3] = b;
常量左值引用int(const& crb)[3] = b;
doesn't compile. 无法编译。 Why? 为什么?
#include<iostream>
int a = 1;
int b[3] = { 1, 2, 3 };
int main(){
int& ra = a; // Ok
int const& cra = a; // Ok
int(&rb)[3] = b; // Ok
int(const& crb)[3] = b; // Doesn't compile
}
Error message emitted by g++: g ++发出的错误消息:
error: expected primary-expression before 'int'
int(const& crb)[3] = b;
^~~
Reference is always constant, you can't change reference. 参考始终是恒定的,您不能更改参考。 int const (& crb)[3] = b;
here we have reference to array of const int, we can also write const int (& crb)[3] = b;
这里我们引用了const int数组,我们也可以写const int (& crb)[3] = b;
It would be the same. 会是一样的。
Pointers have a difference, pointer can be changed 指针有所不同,可以更改指针
const int *p;
- here it is pointer on const int -这是const int的指针
int const *p;
- here it is const pointer on int -这是int上的const指针
const int const *p;
- here it is const pointer on const int -这是const int上的const指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.