1、源代碼裏的do{}while(0)
今天寫到一個代碼:
if(!CCScene::init())
{
return false;
}
後來就去看源代碼:bool CCScene::init()
{
bool bRet = false;
do
{
CCDirector * pDirector;
CC_BREAK_IF( ! (pDirector = CCDirector::sharedDirector()) );
this->setContentSize(pDirector->getWinSize());
// success
bRet = true;
} while (0);
return bRet;
}
看後,我就奇怪,do{}while(0)不就是執行一次麼,是什麼意思?
後來百度,得到如下最佳解釋:
FAQ FROM CSDN:
FAQ/DoWhile0
Why do a lot of #defines in the kernel use do { ... } while(0)?
There are a couple of reasons:
-
(from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0).
-
(from Dave Miller) It gives you a basic block in which to declare local variables.
-
(from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:
#define FOO(x) / printf("arg is %s/n", x); / do_something_useful(x);
if (blah == 2) FOO(blah);
if (blah == 2) printf("arg is %s/n", blah); do_something_useful(blah);;
As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block likedo { ... } while(0), you would get this:
if (blah == 2) do { printf("arg is %s/n", blah); do_something_useful(blah); } while (0);
-
(from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:
if (x > y) exch(x,y); // Branch 1 else do_something(); // Branch 2
But it would be interpreted as an if-statement with only one branch:
if (x > y) { // Single-branch if-statement!!! int tmp; // The one and only branch consists tmp = x; // of the block. x = y; y = tmp; } ; // empty statement else // ERROR!!! "parse error before else" do_something();
The problem is the semi-colon (;) coming directly after the block. The solution for this is to sandwich the block between do and while (0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler. Our if-statement now becomes:
if (x > y) do { int tmp; tmp = x; x = y; y = tmp; } while(0); else do_something();
總的來說,解決了代碼使用宏定義時,要無誤嵌套的目的。
雖然上述代碼沒能很好地體現,但在源代碼裏的大多數宏定義的地方,都是使用了do{}while(0)的結構:
#define CC_SAFE_DELETE(p) do { if(p) { delete (p); (p) = 0; } } while(0)
#define CC_SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); (p) = 0; } } while(0)
#define CC_SAFE_FREE(p) do { if(p) { free(p); (p) = 0; } } while(0)
#define CC_SAFE_RELEASE(p) do { if(p) { (p)->release(); } } while(0)
#define CC_SAFE_RELEASE_NULL(p) do { if(p) { (p)->release(); (p) = 0; } } while(0)
#define CC_SAFE_RETAIN(p) do { if(p) { (p)->retain(); } } while(0)
2、純虛函數 virtual=0
在看代碼的時候,發現C++中有不少地方使用到了純虛函數等於0的。我覺得這樣的意思是在其聲明類中無需實現,需要在子類中實現,類似Java的接口。
後來,百度得到答案如下:
------------>