I recently had a rather strange behaviour of an if statement and wanted to discuss it here. I have a function update() of a class gripper and I have two instances of gripper. The part of the program I am interested in gets executed regularly and I want the function update to be called on both instances once each iteration. The function returns a true once it has finished its task (and a false in the meantime). So I wrote the following:
if(gripper_A_.update() && gripper_B_.update())
//do something
I would expect that update gets called on gripper_A_, and on gripper_B_ and the return values are then connected via the logical and. However, this is not the case as instead once the call of update() on gripper_A_ returns false, the second call of the function, this time on gripper_B_, is not even executed. I didn't get behind this until I tried simply calling the functions one after another like this:
gripper_A_.update();
gripper_B_.update();
I can work around this and find a solution for it, but I find it very unusual. Is the compiler detecting that it doesn't have to execute the second call of the function since the overall value of the logical and will be false anyways? Or did I overlook something else?
Looking forward to your ideas.
Aucun commentaire:
Enregistrer un commentaire