与C+&#x2B中指针的引用相混淆。

发布于 2025-01-19 16:50:42 字数 745 浏览 4 评论 0原文

任务是在没有计算机的情况下计算以下源代码的输出,我想说的是 "123 234 678 678" 因为 ref1 是对值的引用ptr 并且在将 val2 的地址分配给该指针的那一刻,那么 ref1 不应该引用它的值吗?

int main() {
    int* ptr = nullptr;
    int val1 = 123, val2 {234};
    ptr = &val1;
    int& ref1 = *ptr;
    int& ref2 = val2;

    std::cout << ref1 << " ";
    std::cout << ref2 << " ";
    *ptr = 456; //ref1 = 456
    ptr = &val2; //*ptr = 234, why isn't ref1 as well equal to 234?
    *ptr = 678; //*ptr = 678, why isn't ref1 as well equal to 678?

    std::cout << ref1 << " ";
    std::cout << ref2 << "\n";

    return EXIT_SUCCESS;

    //output: 123 234 456 678
}

The task is to calculate the output of the following source code without a computer, which I would say is "123 234 678 678" because ref1 is a reference on the value of ptr and in the moment where the address of val2 is assigned to this pointer, then shouldn't ref1 as well refer to its value?

int main() {
    int* ptr = nullptr;
    int val1 = 123, val2 {234};
    ptr = &val1;
    int& ref1 = *ptr;
    int& ref2 = val2;

    std::cout << ref1 << " ";
    std::cout << ref2 << " ";
    *ptr = 456; //ref1 = 456
    ptr = &val2; //*ptr = 234, why isn't ref1 as well equal to 234?
    *ptr = 678; //*ptr = 678, why isn't ref1 as well equal to 678?

    std::cout << ref1 << " ";
    std::cout << ref2 << "\n";

    return EXIT_SUCCESS;

    //output: 123 234 456 678
}

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

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

发布评论

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

评论(1

半夏半凉 2025-01-26 16:50:43

在此声明之后,

int& ref1 = *ptr;
int& ref2 = val2;

ref1 引用 val1ref2 引用 val2。声明后,不能将引用更改为引用其他变量。

请注意,引用 ref1 并不引用指针 ptr。由于取消引用指向变量 val1 的指针,它引用了变量 val1

所以这些语句

std::cout << ref1 << " ";
std::cout << ref2 << " ";

将输出

121 234

In this statements

*ptr = 456;

the value of the value of the variable val1 ischanged to 456 through using thepointer ptr.

之后指针ptr的值被更改为存储变量val2的地址

ptr = &val2;

,并且变量val2的值更改为< code>678 通过使用指针

*ptr = 678;

所以这些语句

std::cout << ref1 << " ";
std::cout << ref2 << "\n";

现在将输出

456 678

即它们输出引用所引用的变量的值。

在此程序中,由于使用对象地址重新分配了指针,因此使用同一个指针来更改两个不同对象的值。

After this declarations

int& ref1 = *ptr;
int& ref2 = val2;

ref1 refers to val1 and ref2 refers to val2. After the declarations the references can not be changed to refer to other variables.

Pay attention to that the reference ref1 does not refer to the pointer ptr. It refers the variable val1 due to dereferencing the pointer that points to the variable val1.

So these statements

std::cout << ref1 << " ";
std::cout << ref2 << " ";

will output

121 234

In this statement

*ptr = 456;

the value of the variable val1 is changed to 456 through using the pointer ptr.

After that the value of the pointer ptr was changed to store the address of the variable val2

ptr = &val2;

and the value of the variable val2 was changed to 678 through using the pointer

*ptr = 678;

So these statements

std::cout << ref1 << " ";
std::cout << ref2 << "\n";

now will output

456 678

That is they output values of the variables to which the references refer to.

In this program the same one pointer was used to change values of two different objects due to reassignment of the pointer with addresses of the objects.

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