In what order does evaluation of post-increment operator happen?

后端 未结 4 1496
萌比男神i
萌比男神i 2021-01-20 08:36

Given

std::vector objects;
CMyClass list[MAX_OBJECT_COUNT];

Is it wise to do this?

for(unsigned int i = 0;          


        
4条回答
  •  抹茶落季
    2021-01-20 09:10

    As it has been said already, post-incrementing a variable in the same expression it is used yields undefined behaviour. However, if you wish to keep the compact form, you could introduce a sequence point and go for

    for(unsigned int i = 0; i < objects.size(); list[i] = objects.at(i), i++);
    

提交回复
热议问题