【问题标题】:c++ using 'this' in node treec ++在节点树中使用'this'
【发布时间】:2013-03-05 09:57:24
【问题描述】:

我在 main 中创建根。然后在另一个 .cpp 我做类似的事情

TreeNode * current = this;

如果我这样做了

current = current->right;

这样我就可以下树了。它会改变“this”的含义吗?

【问题讨论】:

  • 很确定我想通了。我做了一个 changeThis() 来自己尝试,它并没有改变 'this' 中的任何内容。

标签: c++ tree nodes


【解决方案1】:

它会改变“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 指向的对象。

【讨论】:

    【解决方案2】:

    不,您正在复制this 的值到current。更改 current 不会影响 this。无论如何,您都无法更改 this 的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      相关资源
      最近更新 更多