[SOLVED] Understanding Smart pointer CCPointer

I came across an article that employs CCPointer I wanted to know if there are any tutorials on CCPointer. I am familiar with C++11 smart pointers however I could not find anything regarding CCPointer. Any tips on this would be helpful.

which article? I’m not familiar wIth CCPointer. Cocos2d-x v3 uses Ref and RefPtr

This is the article

Yeah… that seems to be based on v2.x.

What version are you using ? v3 ?
If so, you should know Nodes and other objects are subclass of Ref, which is a Reference count basically.
If you want to use those objects in vectors and others, you should use cocos2d container classes like:
cocos2d::Vector<>, cocos2d::Map<>, etc.

But if you want to use STL containers with cocos2d Ref objects, you should wrap them in a RefPtr object.

Yes I am using Vesrion 3.0. I did not know about

But if you want to use STL containers with cocos2d Ref objects, you should wrap them in a RefPtr object.

I was not aware of that. Can you give an example of how I could create a RefPtr that points to a sprite pointer ?

I’ve tested this code, but it should work:

// create the shared data pointer
RefPtr<Sprite> ref(Sprite::create("hello.png"));

// add it to your std vector
_vector.push_back(ref);

See the RefPtrTest.cpp in cocos2d-x for a working sample

1 Like

thank you for the example