引用变量和指针问题

发布于 2024-11-28 08:09:25 字数 1135 浏览 0 评论 0原文

我有一个指向整数变量的指针。然后我将该指针分配给一个引用变量。现在,当我更改指针以指向其他整数变量时,引用变量的值不会改变。谁能解释为什么?

int rats = 101;
int * pt = &rats;
int & rodents = *pt;                                // outputs    
cout << "rats = " << rats;                          // 101
cout << ", *pt = " << *pt;                          // 101
cout << ", rodents = " << rodents << endl;          // 101
cout << "rats address = " << &rats;                 // 0027f940
cout << ", rodents address = " << &rodents << endl; // 0027f940
int bunnies = 50;
pt = &bunnies;

cout << "bunnies = " << bunnies;                    // 50
cout << ", rats = " << rats;                        // 101  
cout << ", *pt = " << *pt;                          // 50
cout << ", rodents = " << rodents << endl;          // 101
cout << "bunnies address = " << &bunnies;           // 0027f91c
cout << ", rodents address = " << &rodents << endl; // 0027f940

我们将 pt 分配给兔子,但啮齿类动物的值仍然是 101。请解释原因。

I have a pointer which is pointing to an integer variable. Then I assign this pointer to a reference variable. Now when I change my pointer to point some other integer variable, the value of the reference variable doesn't change. Can anyone explain why?

int rats = 101;
int * pt = &rats;
int & rodents = *pt;                                // outputs    
cout << "rats = " << rats;                          // 101
cout << ", *pt = " << *pt;                          // 101
cout << ", rodents = " << rodents << endl;          // 101
cout << "rats address = " << &rats;                 // 0027f940
cout << ", rodents address = " << &rodents << endl; // 0027f940
int bunnies = 50;
pt = &bunnies;

cout << "bunnies = " << bunnies;                    // 50
cout << ", rats = " << rats;                        // 101  
cout << ", *pt = " << *pt;                          // 50
cout << ", rodents = " << rodents << endl;          // 101
cout << "bunnies address = " << &bunnies;           // 0027f91c
cout << ", rodents address = " << &rodents << endl; // 0027f940

We assigned pt to bunnies, but the value of rodents is still 101. Please explain why.

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

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

发布评论

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

评论(1

将军与妓 2024-12-05 08:09:25

该行

int & rodents = *pt;

正在创建对 pt 所指向的内容的引用(即 rats)。它不是对指针 pt 的引用。

稍后,当您将 pt 指定为指向 bunnies 时,您不会期望 rodents 引用发生变化。

编辑:为了说明@Als点,请考虑以下代码:

int value1 = 10;
int value2 = 20;
int& reference = value1;
cout << reference << endl; // Prints 10
reference = value2; // Doesn't do what you might think
cout << reference << endl; // Prints 20
cout << value1 << endl; // Also prints 20

第二个引用赋值不会更改引用本身。相反,它将赋值运算符 (=) 应用于所引用的事物,即 value1

reference 将始终引用 value1 并且无法更改。

一开始理解起来有点困难,所以我建议您看一下 Scott Meyer 的优秀书籍 有效的 C++更高效的 C++。他对这一切的解释比我好得多。

The line

int & rodents = *pt;

is creating a reference to what pt is pointing to (i.e. rats). It's not a reference to the pointer pt.

Later, when you assign pt to point to bunnies, you would not expect the rodents reference to change.

EDIT: To illustrate @Als point, consider the following code:

int value1 = 10;
int value2 = 20;
int& reference = value1;
cout << reference << endl; // Prints 10
reference = value2; // Doesn't do what you might think
cout << reference << endl; // Prints 20
cout << value1 << endl; // Also prints 20

The second reference assignment does not change the reference ltself. Instead, it applies the assignment operator (=) to the thing referred to, which is value1.

reference will always refer to value1 and cannot be changed.

It's a little tricky to get your head around at first, so I recommend you take a look at Scott Meyer's excellent books Effective C++ and More Effective C++. He explains all this much better than I can.

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