iDevGames Forums
objective-c and c++ question - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: objective-c and c++ question (/thread-2616.html)

objective-c and c++ question - OptimisticMonkey - May 27, 2008 07:25 PM

I am building an app targeting the iPhone. I need to maintain a group of entities. The group grows dynamically.

Historically I have used an STL vector. It grows automatically and performs well. But now I am moving more and more to cocoa and obj-c. In particular, I am using archiving feature of NSCoding. So I have an objective-c based object that represents my level.

I need to store a group of entities. Each entity is often just a c-struct (I am using chipmunk physics.)

Unfortunately, embedding an stl vector as an instance variable in my obj-c class generates warnings about not calling constructors and destructors. I have tried using a pointer to a vector, and manually constructing and destructing, but I seem to be having some issues - especially during encoding and decoding.

NSArray seems to only be for storing obj-c objects.

So my question is:
Should I continue trying to use a c++ stl vector as an instance variable, or is it better to fallback to old school c-arrays? I hesitate allocating big arrays with wasted space, but that might just be the easiest thing to do.

Thanks for any advice/pointers/tips

objective-c and c++ question - OneSadCookie - May 27, 2008 07:37 PM

You can make the Obj-C++ thing work. -fobjc-call-cxx-cdtors may help (see "man gcc" for details), or you can use "placement new" to call the objects' constructors yourself, or you can use a pointer-to-c++-object member, and use new and delete to manage it explicitly.

objective-c and c++ question - reubert - May 27, 2008 09:05 PM

Obviously NSArray is going to be by far the easiest. All you need to do is wrap that Entity in an NSObject subclass and you get all the happiness that NSArray provides.

The only reason for not using it really is if you can't afford the performance overhead. In that case you can always realloc to deal with dynamic sized c arrays, or like you said just waste memory. At least that way you know your limits and won't accidentally run out of memory on an iPhone Wink

How many 'entities' do you expect to have?

objective-c and c++ question - TomorrowPlusX - May 28, 2008 12:59 PM

I agree with Reubert.

I've been down this route many times ( not on the iPhone, admittedly ). You'll probably find it easiest to define an ObjC class which encapsulates the chipmunk physics struct, and which has an accessor for that struct. 1 minute of work, and you don't need to worry any more.

objective-c and c++ question - OptimisticMonkey - May 28, 2008 02:51 PM

Thanks for the replies - I ended up getting it to work with a pointer to an STL vector and manually running new and delete.

I think I may still rewrite using obj-c object and NSMutableArray. I find myself wanting to use cocoa and obj-c more and more.

Thanks again for your replies