CCArray usage questions

Hi people! I’d just like to understand how the CCArray works and how people use it normally, it’s been quite confusing and I found myself reverting back to the C++ Array instead =s

When you add objects into the CCArray what happens to it’s type exactly? When and how does it turn into a CCObject?
Why can’t I use any sort of typecast on the CCObject and cast the type back to an integer?

When you add an object in a CCArray, its type doesn’t change at all (that’s not even possible in C++ anyway). It’s still the same type, you just need to cast it again to the right type to use it without compile error.

An integer doesn’t derive from CCObject, so it’s normal you can’t cast integer <=> CCObject. However, you can use CCInteger in a CCArray, or any type that derives from CCObject.

How do I work with it ? I just don’t use any object that doesn’t derive from CCObject and sugarcoat everything with static casts.

Thanks for the clarification! I think there’s still some way to go for me to work this out, I’ve added some enums to a CCArray and trying to get them back out again seems quite complex =s

Oh, is this legal syntax?

CCObject x = array[0];
//instead of
CCObject x = array->objectAtIndex(0);

An enum is an integer in disguise, nothing more. You still need to use a CCInteger. Here is some code sample, on the top of my head:

typedef enum
{
 NoValue,
 Value
}MyEnum;

CCArray* array = CCArray::create();
array->addObject(CCInteger::create((int)NoValue));

MyEnum result = (MyEnum) ((CCInteger*)array->objectAtIndex(0))->getValue();
//result is equal to 'NoValue' 

You can just use std::vector or std::list or even normal arrays for non-CCObject objects.

After some testing, I think I completely misunderstood what a CCArray is, clarity on these questions would help immensely:

buffer->initWithCapacity(20);

I understand that initializing a CCArray this way turns it into an autorelease object, is there any point in using this method in a class’s init() rather than

buffer = new CCArray(20);

at all then?

What does the elements of a CCArray initialize to?
I tried using quite a few methods such as

buffer->replaceObjectAtIndex(i, ((CCInteger*)i));

and it outright fails me :frowning:

is

CCObject x = buffer[0];
//instead of
CCObject x = buffer->objectAtIndex(0);

//and
*buffer[0] = objectX;

Valid syntax at all?

I’m currently looking through the CCArray.h file, and it looks like it’s just a wrapper around ccCArray.h, hopefully I can understand what’s really going on by the end of this hiking mission =s

  1. I never use CCArray::create(). I always use new and just release them on my class’ destructor.
  2. CCArray::create() creates these issues.
  3. No. The correct syntax is CCObject * object = myArray -> objectAtIndex( i ); CCArray contains CCObject * objects, not CCObject. Those two are different.

If you’re just going to store integers, I suggest you use std::vector instead.

Create is a wrapper around new + autorelease. You want to use create to have an object with refcount = 0, and new for an object with refcount = 1. Note that you don’t need to provide a capacity : it’s a hint to how many elements there will be (contrary to a static array which must be the actual size). Even then, the array will resize itself if you add more elements. It’s mainly useful for performance reasons if you know straight off you are going to need a lot of elements and want to “reserve” that space.

When you create a CCArray, it doesn’t actually fill itself with any object. You need to add your objects, so it’s normal it fails if you try it on an empty array.

The [] operator isn’t overloaded, so no, it’s not a valid syntax. You would need to add the overload in cocos2dx. You could do that and send a pull request. Otherwise, it defaults to the standard behavior of C++, which is pointer arithmetic : don’t do it, it will probably crash your app or do weird stuff.

To really get it, you should look at NSArray : cocos2dx is mainly a rewrite of cocos2d-iphone plus some useful features of Objective-C : reference counting, NSObject, NSArray, NSDictionary and so on …