【发布时间】:2013-03-05 09:57:24
【问题描述】:
我在 main 中创建根。然后在另一个 .cpp 我做类似的事情
TreeNode * current = this;
如果我这样做了
current = current->right;
这样我就可以下树了。它会改变“this”的含义吗?
【问题讨论】:
-
很确定我想通了。我做了一个 changeThis() 来自己尝试,它并没有改变 'this' 中的任何内容。
我在 main 中创建根。然后在另一个 .cpp 我做类似的事情
TreeNode * current = this;
如果我这样做了
current = current->right;
这样我就可以下树了。它会改变“this”的含义吗?
【问题讨论】:
它会改变“this”的含义吗?
否。
current 不是this 的别名,无论如何您都无法更改this 指针。
这就是你正在做的事情。假设this 指向某个对象,并将其称为OBJECT1。一开始,你有这种情况:
[ this --------> OBJECT1 ] (this points to OBJECT1)
在你这样做之后
TreeNode * current = this;
你有这种情况:
[ this --------> OBJECT1 ] (this points to OBJECT1)
[ current -----> OBJECT1 ] (current also points to OBJECT1)
在你这样做之后......
current = current->right;
你有这种情况:
[ this --------> OBJECT1 ] (this still points to OBJECT1)
[ current -----> OBJECT2 ] (current now points to a different object)
其中OBJECT2 是指向或由OBJECT1->right 指向的对象。
【讨论】:
不,您正在复制this 的值到current。更改 current 不会影响 this。无论如何,您都无法更改 this 的值。
【讨论】: