为什么更改引用指向的内容不会引发错误?

发布于 2024-11-27 15:17:56 字数 232 浏览 1 评论 0原文

我的 C++ 研究已经到了关于参考文献的阶段。它规定了以下规则:

一旦初始化了对某个对象的引用,就不能将其更改为引用另一个对象。

Iv 编写了一个简短的代码(按照练习中的要求),旨在证明该规则是正确的。

int y = 7;
int z = 8;

int&r = y;
r = z;

有人可以解释为什么这段代码编译时没有任何错误或警告吗?

Iv got to the stage in my c++ study concerning references. It states the following rule:

Once a reference is initialized to an object, it cannot be changed to refer to another object.

Iv wrote a short code (as asked to in an exercise) that is meant to prove this rule correct.

int y = 7;
int z = 8;

int&r = y;
r = z;

Can someone explain why this code compiles without any errors or warnings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烏雲後面有陽光 2024-12-04 15:17:56

r = z 不会改变 r “指向”的内容。它将z 的值赋给r 指向的对象。

以下代码与您的代码执行相同的操作,但使用指针而不是引用:

int y = 7;
int z = 8;

int* p = &y; // p points to y
*p = z;      // assign value of z to the object pointed to by p (which is y)

r = z does not change what r "points to." It assigns the value of z to the object pointed to by r.

The following code does the same thing as your code, but using pointers instead of references:

int y = 7;
int z = 8;

int* p = &y; // p points to y
*p = z;      // assign value of z to the object pointed to by p (which is y)
心是晴朗的。 2024-12-04 15:17:56

它不会为其他内容创建引用别名,但会更改 r 包含的内容的值。

int&r = y;

r是对y的引用,

r = z;

改变y的值&通过将 z 的值分配给 r & 将 r 的值赋给 z 的值因此y

It does not make the reference alias to something else but it changes the value of what r contains.

int&r = y;

ris reference to y

r = z;

changes value of y & r to value of z by assigning value of z to r & hence y.

圈圈圆圆圈圈 2024-12-04 15:17:56
int&r = y;
r = z;

它不会改变参考。相反,它更改引用变量指向的。引用变量只是 y 的另一个名称。因此,r=z 相当于 即

y = z;

r=z 更改了 y

引用变量无法以任何方式重置为引用另一个变量。

int&r = y;
r = z;

It does NOT change the reference. Rather it changes the value pointed to by the reference variable. The reference variable is just yet another name of y. So r=z is equivalent to

y = z;

That is, r=z changes the value of y.

Reference variable cannot be reset to refer to another variable, in any way.

泪冰清 2024-12-04 15:17:56

您不会更改引用;而是会更改引用。您正在为引用的对象设置一个新值。在此示例之后,您应该注意到 y==8。

You're not changing the reference; you're setting a new value to the referred object. After this example you should note that y==8.

旧人 2024-12-04 15:17:56

当您执行r = z时,您并没有重新定位引用,而是将z的值复制y中>。

When you do r = z you are not reseating the reference, instead you are copying the value of z into y.

薄暮涼年 2024-12-04 15:17:56

我在学习<时遇到了同样的问题。第11章。
这是我的理解代码:第二个编译错误似乎无法模拟。但可以理解。你可以画图看看x、y、z和ref2指向什么。

/**
Write a program in which you try to
    (1) Create a reference that is not initialized when it is created.
    (2) Change a reference to refer to another object after it is initialized.
    (3) Create a NULL reference.
**/
#include <iostream>

using namespace std;

int main() {
    // 1 
    //int& ref1;      // compile error: 

    int x = 10;
    int& ref2 = x;
    cout << "x = " << x << endl;
    cout << "ref2 = " << ref2 << endl << endl;

    // 2 
    int y = 20;
    ref2 = y;   // 这里没有编译错误,ref2的指向并未改变,其指向的空间由10改变为20
    cout << "ref2 = " << ref2 << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl << endl;

    int z = 30;
    ref2 = z;
    cout << "ref2 = " << ref2 << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;

    // 3 
    //int& ref3 = NULL;   // compile error: 
    return 0;
}

I faced the same issue when study <<thinking in c++> charpter11.
here is my understanding code: the second compile error seems can not be simulated. but can understand. you can draw a picture to see what x, y, z, and ref2 point to.

/**
Write a program in which you try to
    (1) Create a reference that is not initialized when it is created.
    (2) Change a reference to refer to another object after it is initialized.
    (3) Create a NULL reference.
**/
#include <iostream>

using namespace std;

int main() {
    // 1 
    //int& ref1;      // compile error: 

    int x = 10;
    int& ref2 = x;
    cout << "x = " << x << endl;
    cout << "ref2 = " << ref2 << endl << endl;

    // 2 
    int y = 20;
    ref2 = y;   // 这里没有编译错误,ref2的指向并未改变,其指向的空间由10改变为20
    cout << "ref2 = " << ref2 << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl << endl;

    int z = 30;
    ref2 = z;
    cout << "ref2 = " << ref2 << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;

    // 3 
    //int& ref3 = NULL;   // compile error: 
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文