【发布时间】:2021-08-31 19:01:05
【问题描述】:
我正在学习 C++,但有点停顿。我正在尝试使用基于范围的 for 循环遍历向量并更新属于它的每个对象的属性。循环位于更新函数内部。第一次启动时,它工作正常;我可以看到该属性在向量的每个成员上都得到了更新。但是,下次启动 for 循环时,它仍在更新原始数据,就好像上次运行并没有真正更新源值一样。我的范围声明配置正确吗?指针对我来说仍然有点神秘。总的来说,我会非常感谢您的帮助!
for (Point &point : points)
{
Vector3 position = point.position;
if (position != destination)
{
Vector3 move = Vector3::Zero;
if (position.x > destination.x)
move.x -= 1.0;
if (position.x < destination.x)
move.x += 1.0;
if (position.y > destination.y)
move.y -= 1.0;
if (position.y < destination.y)
move.y += 1.0;
if (position.z > destination.z)
move.z -= 1.0;
if (position.z < destination.z)
move.z += 1.0;
position += move;
}
}
【问题讨论】:
-
Vector3 position = point.position;看起来非常像复制某些东西。 -
谢谢!认为以某种方式工作不同。那行得通。明显地。 :)