objective-c and c++ question

Posts: 86
Joined: 2008.04
Post: #1
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
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Posts: 320
Joined: 2003.06
Post: #3
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?

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Posts: 1,199
Joined: 2004.10
Post: #4
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.
Quote this message in a reply
Posts: 86
Joined: 2008.04
Post: #5
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Objective C question Wayrath 8 11,330 Apr 15, 2010 07:25 AM
Last Post: Wayrath
  Objective-C property question. proxus 4 6,762 Feb 26, 2009 10:48 PM
Last Post: Josh
  Basic Objective-C question elliptic 7 8,227 Aug 1, 2008 12:03 PM
Last Post: SethWillits
  Beginner - Objective-C/C++/Engines question LeChuck 8 7,869 Nov 26, 2007 04:26 PM
Last Post: Duane