cocos2d-x 问题集。

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);
    

    Now imagine using it like:

    if (blah == 2)
            FOO(blah);
    

    This interprets to:

    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);
    

    Which is exactly what you want.

  • (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的接口。

       后来,百度得到答案如下:

------------>

       在C++中的一种函数申明被称之为:纯虚函数(pure virtual function).它的申明格式如下:
       class CShape
       {
       public:
            virtual void Show()  =0;
       };
       注意红色部分,在普通的虚函数后面加上"  =0"这样就声明了一个pure virtual function.
 
           在什么情况下使用纯虚函数(pure vitrual function)?
       1,当想在基类中抽象出一个方法,且该基类只做能被继承,而不能被实例化;
       2,这个方法必须在派生类(derived class)中被实现;
       (原文请参看: http://www.oschina.net/question/565065_86228
< ------------
      
        综上答案:也就如我的猜测一样,类似Java的接口方法一样,其所在类只能被继承(C++中不存在implements),且该方法必须在派生类中被实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值