Projects > cpp > Issues > Refactor #2518

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

Create Issue on Github

Hybrid approach: Include cocos2d SmartPtr in the code

Refactor #2518 [Rejected]
ricardo 2013-08-10 00:24 . Updated about 3 years ago

Include this code in cocos2d-x:

Code based on this code:

The license should be this one:

and the author is: Ahmed S. Hefny

template < class T >
class RefCountPtr
    //Construct using a C pointer
    //e.g. RefCountPtr< T > x = new T();
    RefCountPtr(T* ptr = NULL)
    : _ptr(ptr)
        if(ptr != NULL) {ptr->retain();}

    //Copy constructor
    RefCountPtr(const RefCountPtr &ptr)
    : _ptr(ptr._ptr)
        if(_ptr != NULL) {_ptr->retain();}

        if(_ptr != NULL)

    //Assign a pointer
    //e.g. x = new T();
    RefCountPtr &operator=(T* ptr)
        //The following grab and release operations have to be performed
        //in that order to handle the case where ptr == _ptr
        //(See comment below by David Garlisch)
        if(ptr != NULL) {ptr->retain();}
        if(_ptr != NULL) {_ptr->release();}
        _ptr = ptr;
        return (*this);

    //Assign another RefCountPtr
    RefCountPtr &operator=(const RefCountPtr &ptr)
        return (*this) = ptr._ptr;

    //Retrieve actual pointer
    T* get() const
        return _ptr;

    //Some overloaded operators to facilitate dealing with an RefCountPtr
    //as a conventional C pointer.
    //Without these operators, one can still use the less transparent
    //get() method to access the pointer.
    T* operator->() const {return _ptr;}        //x->member
    T &operator*() const {return *_ptr;}        //*x, (*x).member
    explicit operator T*() const {return _ptr;}     //T* y = x;
    explicit operator bool() const {return _ptr != NULL;}   //if(x) {/*x is not NULL*/}
    bool operator==(const RefCountPtr &ptr) {return _ptr == ptr._ptr;}
    bool operator==(const T *ptr) {return _ptr == ptr;}

    T *_ptr;    //Actual pointer

class HelloWorld : public Node {
    typedef RefCountPtr ptr;

    virtual ~HelloWorld() {
        log("HelloWorld: Destructor");

    static ptr create() {
        auto var = ptr( new HelloWorld);
        return var;
ricardo 2013-08-10 00:26
  • Tracker changed from Bug to Refactor
  • Priority changed from Normal to High
ricardo 2013-08-13 04:00
  • Status changed from New to Rejected

Hybrid approach won’t be used.

Atom PDF

Start date:2013-08-10
Due date:
% Done:


Target version:3.0-alpha0

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