CCScene* GameLayer::scene() {
CCScene * scene = NULL;
do {
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
GameLayer *layer = GameLayer::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
do-while
这段代码中block的含义是什么?
CC_BREAK_IF
是的宏if(condition) break
。(编辑:我已经确认是。)
这是用于结构化goto的惯用法:
do {
if (!condition0) break;
action0();
if (!condition1) break;
action1();
} while(0);
do...while(0);
存在只是为了让break语句跳过部分代码。
这类似于:
if (!condition0) goto end;
action0();
if (!condition1) goto end;
action1();
end:
除非它避免使用goto。
使用这些惯用法之一是为了避免嵌套if
:
if (condition0) {
action0();
if (condition1) {
action1();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句