我对这个问题很困惑,不知道怎么问这个问题。 就像 -
current = head;
如果“head”的值稍后发生变化
head = temp->next;
“current”的值也会改变吗?
最佳答案
这取决于current
(和head
)的类型。
例如,在:
Node *head = get_head_from_somewhere();
Node *¤t = head;
head = head->next;
current
是 head
的别名,因此更改 head
(将其前进到指向下一个节点)也会影响 current
。它们总是具有相同的值。
事实上,虽然它们都在上面声明的范围内,但 assert(head == current)
总是会成功。
不过
Node *current = head;
创建一个新的独立指针,它指向与 head
相同的位置。推进 head
不会改变这里的 current
。
关于c++ - 赋值后值可以改变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44759614/