列表中的C ++对象变量在迭代期间不会更新

时间:2018-01-16 19:14:39

标签: c++ list object iteration

当遍历列表时,列表中对象的变量不会更新,通过调试看起来变量只是暂时更新,直到循环结束。为什么是这样?我一直在找很长时间。

for (std::list<GUI>::iterator it = allguis.begin(); it != allguis.end(); ++it) {
    GUI gui = *it;
    speed = 5
   if (gui.activated) {
    gui.position.x = gui.position.x + gui.distanceX * speed;
    gui.position.y = gui.position.y + gui.distanceY * speed;
           }
   }                

GUI类:

class GUI
{
public:
    sf::Vector2f position = sf::Vector2f(0,0);
    int sizex;
    int sizey;
    bool activated;
    float rotation;
    int damage;
    float distanceX;
    float distanceY;

};

1 个答案:

答案 0 :(得分:4)

list.layoutManager = object: LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) { override fun onInterceptFocusSearch(focused: View?, direction: Int): View? { if (direction == View.FOCUS_DOWN) { val pos = getPosition(focused) if (pos == itemCount-1) return focused } if (direction == View.FOCUS_UP) { val pos = getPosition(focused) if (pos == 0) return focused } return super.onInterceptFocusSearch(focused, direction) } } 创建一个局部变量,该变量使用存储在容器中的值的副本进行初始化。你应该使用refence:

GUI gui = *it;

或者你可以使用C ++ 11风格的循环:

GUI & gui = *it;