Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;:对嵌套范围使用中使用的常量引用的混淆_C++_C++11 - Fatal编程技术网

C++ c++;:对嵌套范围使用中使用的常量引用的混淆

C++ c++;:对嵌套范围使用中使用的常量引用的混淆,c++,c++11,C++,C++11,我对下面的内容感到困惑。让我简单解释一下 int ia [3][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} }; for (const int (&r)[4] : ia) for (int &x : r) --> this has err

我对下面的内容感到困惑。让我简单解释一下

int ia [3][4] =  {
                     {0, 1, 2, 3},
                     {4, 5, 6, 7},  
                     {8, 9, 10, 11}  
                 };

for (const int (&r)[4] : ia)
      for (int &x : r) --> this has error
             x = 1;
错误是“将'const int'类型的值绑定到对'int'类型的引用时,会删除'const'限定符”


引用r指向int数组,但它是常量引用。这是可以理解的。内部for循环使用r,它将迭代内部数组。r是数组的常量引用,但不是每个项。那么为什么会有错误呢

我可以用下面的代码替换,它可以正常工作

 for (const int (&r)[4] : ia)
            for (int x : r) --> this has error
                      x = 1;
这我理解。
非常感谢您的帮助。

您不能将非
常量
引用带到
常量
数组的元素
r
是对
const
数组的引用,因此不能对其元素进行非
const
引用

使用时,请尝试使用(int(&r)[4]:ia)

for (const int (&r)[4] : ia)
for (int x : r)
您现在有了一个数组,指向
const
int
。不能将非
const
引用绑定到这些元素之一,因为这违反了cont正确性

另一方面,当你使用

for (const int (&r)[4] : ia)
for (int x : r)

你不再做推荐人,而是复制一份。您始终可以将
常量
元素复制到非
常量
元素中,因为您无法从副本中修改
常量
元素。

不清楚您在哪里感到困惑;下面是一组可能的混淆点

  • 对常量数组的引用与对常量元素上数组的引用相同

  • <> > C++程序员表示const引用时,它们指的是const的引用;除了对const的引用之外,没有const引用

  • 无法从对数组的常量引用中获取非常量元素引用

  • 您的代码尝试这样做。编译器说“不”


阵列和基于范围的循环只是混淆了问题。显示相同错误的更简单代码如下:

int main() {
    int x = 0;
    const int& y = x;
    int& z = y;

}
导致:

error: binding value of type 'const int' to reference to type 'int' drops 'const' qualifier
    int& z = y;
         ^   ~
即使
x
不是
const
,并且使用引用就像使用原始变量一样(大多数情况下),您也无法从const引用中获得非const引用

也很容易理解为什么不允许这样做。考虑

void foo(const int& x);
foo
无法更改所传递参数的值,但如果允许上述操作,
foo
可以实现如下:

void foo(const int& x) {
    int& y = x;        // now I am allowed to change it
    y++;
}

这将明显破坏constness,因此它是不允许的。

您不能声明对const的非const引用。它将打破
const
规则,因为您可以更改该常量的值“引用r到int数组,但它是一个const引用”。所有引用都不能反弹,因此所有引用都是“const”,因此您实际上不会为它们编写
const
。如果你添加
const
,那意味着完全不同的东西。为什么使用int x:r有效而int&x:r无效?我们已经写了3遍了,请阅读答案。const参考是r可以做的。数组ia不是常量。因此,每个项的迭代不是const reference所能做的。那么为什么int可以工作?@yapkm01即使引用的对象不是const,也不能使用
const
引用来修改它
const
指针和引用不能用于修改引用的对象,无论该对象是否为
const
。这就是为什么它们是常量。拥有
intx
是有效的,因为您不再需要参考。这是一个副本,你可以随意修改它。