对类型'const int *'的非const左值引用不能绑定到不相关的类型'int *

int main(int argc, char const *argv[])
{    
   int *i;
   const int * &j = i;
}

This code gives the error error: non-const lvalue reference to type 'const int *' cannot bind to a value of unrelated type 'int *

But it's not clear to me why this is not allowed. I defined j to be a reference to a pointer to a constant int. So if I set it equal to i, aren't I saying that j is a reference of i and we are not allowed to modify the int pointed to by i through j?

评论
  • 离别yi刻
    离别yi刻 回复
    因此,如果我将其设置为等于i,不是说j是i的引用

    那就是你想说的。但是我的类型是不相容的,所以这是不能说的。

    并且我们不允许修改i到j指向的int?

    If you had a pointer to const, then you couldn't modify the pointed int. And you could refer to such pointer with j. But you haven't created such pointer.