Projects > cpp > Issues > Refactor #3096

We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.

Create Issue on Github

Remove CREATE_FUNC macro, use Template

Refactor #3096 [Closed]
zhen 2013-10-31 21:22 . Updated about 2 years ago

With c++11 it will be better to have a unique create function using templates instead of use a CREATE_FUNC macro in each class.

    namespace cocos2d{
        template< typename T >
        T * create(){
            T * pRet = new T;
            if( pRet and pRet->init() ){
                pRet->autorelease();
            }else if( pRet ){
                delete pRet;
                pRet = nullptr;
            }
            return pRet;
        }
    }

And then, we change from:
@ MyNewScene * scene = MyNewScene::create();@

to:
@ auto scene = cocos2d::create<MyNewScene>();@

And we remove the need for remember to add CREATE_FUN(MyNewScene); in the class.

We also can forward template parameters to allow parameters in the Object constructor:

    template< typename T, typename ...Args >
    T * createCC(){
        T * pRet = new T(std::forward...);
        if( pRet and pRet->init() ){
            pRet->autorelease();
        }else if( pRet ){
            delete pRet;
            pRet = nullptr;
        }
        return pRet;
    }
zhen 2013-11-01 06:04

UPDATED Constructor argument list fixed: I forget to pass argument list the object contructor.

    template< typename T, typename ...Args >
    T * createCC( Args&& ... args ){
        T * pRet = new T(std::forward(args)...);
        if( pRet and pRet->init() ){
            pRet->autorelease();
        }else if( pRet ){
            delete pRet;
            pRet = nullptr;
        }
        return pRet;
    }
walzer@cocos2d-x.org 2014-10-08 02:52

Redmine issue system is closed, we are using github issue system instead.

This issue was moved to https://github.com/cocos2d/cocos2d-x/issues/8245

Atom PDF

Status:Closed
Start date:2013-10-31
Priority:Low
Due date:
Assignee:-
% Done:

0%

Category:all
Target version:-

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.