Update each value in a std::list with a foreach loop C++

前端 未结 1 1284
天涯浪人
天涯浪人 2021-01-28 21:05

I\'ve got a std::list in C++ and I\'m trying to use a for(Type t : list) operation to update the value of each object. So I have a list called balls a

相关标签:
1条回答
  • 2021-01-28 21:29

    You're taking a copy of original object, use a reference

    for(OpenGLView::AssetInstance& ball : balls)
                ball.position = calculateBallPosition(ball);
    

    Or Simply

    for( auto& ball : balls)
                ball.position = calculateBallPosition(ball);
    
    0 讨论(0)
提交回复
热议问题