Do {} while (0) interpretation in the Cocos2d-x
This article for beginners of Cocos2d-x to explain do {} while (0) Usefulness!
Its significance lies in its ability to implement complicated definitions and versatility, which can be applied in any situation.
# Define CC_BREAK_IF (cond) if (cond) break
We use the CC_BREAK_IF macro definition as an example to summarize the following points:
(1) avoid empty macro definitions
For example, if: cond = NULL, it is: # define CC_BREAK_IF (NULL) if (NULL) break
(2) If the macro appears after the judgment statement, it can be implemented as a whole:
# Define lizi ()\
Fangfa1 ();\
Fangfa2 ();
In the following situation:
If (NULL = dJ)
Lizi ();
Fangfa1 () and fangfa2 () will not be executed at the same time, and this is obviously not the purpose of program design.
(3) when this occurs, why must I use the do {} while (0) statement?
Take a look at the following example:
# Define switch (x, y) {int temp; temp = x; x = y; y = temp ;}
If (x> y)
Switch (x, y );
Else // will cause an error, because the subsequent statement will be redundant.
Use do {} while (0) to include it and become an independent statement to avoid the above problems.