我在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。
不,您正在复制thisto的值current。改变current不会影响this。this无论如何,您都无法更改 in 的值。