分配后可以改变价值吗?(Can value change after assignment?)

编程入门 行业动态 更新时间:2024-10-24 12:30:32
分配后可以改变价值吗?(Can value change after assignment?)

我对这个问题感到困惑,不知道如何问这个问题。 喜欢如果 -

current = head;

如果“head”的值稍后变化,如果

head = temp->next;

“当前”的价值也会改变吗?

I'm confused about this question and don't know how to ask this. Like if -

current = head;

if value of "head" changes later like

head = temp->next;

will the value of "current" also change?

最满意答案

这取决于current (和head )的类型

例如,在:

Node *head = get_head_from_somewhere(); Node *&current = head; head = head->next;

current别名是head ,因此改变head (使其前进到指向下一个节点)也会影响current 。 它们总是具有相同的价值。

实际上,虽然它们都在上面assert(head == current)范围内,但assert(head == current)将始终成功。

然而

Node *current = head;

创建一个新的独立指针,它只是开始指向与head相同的位置。 推进head不会改变current 。

That depends on the type of current (and of head).

For example, in:

Node *head = get_head_from_somewhere(); Node *&current = head; head = head->next;

current aliases head, so changing head (advancing it to point at the next node) also affects current. They both always have the same value.

In fact, while they're both in scope as declared above, assert(head == current) will always succeed.

However

Node *current = head;

creates a new and independent pointer, which just starts life pointing at the same place as head. Advancing head won't change current here.

更多推荐

本文发布于:2023-08-07 19:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分配   价值   assignment   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!