PDA

View Full Version : C++ to Objective C




hydrorush
Aug 7, 2007, 04:07 AM
i was wondering if there was something comparable to the STL for C++ for objective c. Is there some way of have templating your implementations of adt's.



kainjow
Aug 7, 2007, 08:10 AM
Not sure exactly what you mean, but you can use C++ directly within Objective-C:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_4_section_10.html

Soulstorm
Aug 7, 2007, 11:33 AM
i was wondering if there was something comparable to the STL for C++ for objective c. Is there some way of have templating your implementations of adt's.

What do you mean by saying "adt's"?

Objective C's Foundation Framework has classes that do the same work with STL's classes.

For example, look at the C++ - ObjC equivalents:

strings -> NSString
Vectors ->NSArray and NSMutableArray
Maps -> NSDictionary

Now, queues, stacks, etc are derivatives of vector, and in ObjC will not have a significant performance advantage of being developed, since NSArray already stores elements in a dynamic manner, using pointers. And it accepts any derivative of NSObject. There are some fundamental differences, though, for example in an NSArray in location 1 you can put an NSString, but in location 2, you can put an NSDictionary.

If you want to use the STL, you can use C++ within ObjC. But let me give you some advice. Use C++ with ObjC only if you have to. Even if you want to port a program from C++ to ObjC, things can be really messy and you could end up writing spaghetti code if you don't know what you're doing. ObjC more than matches the capabilities of C++. Use C++ only for accessing low-level mechanics of an application.

iSee
Aug 7, 2007, 01:16 PM
FYI, Soulstorm, adts (or ADTs) are "Abstract Data Types" which you've correctly guessed the meaning of.

Objective-C doesn't have templates (I don't have direct knowledge of what's on tap for Objective-C 2.0, but I don't believe any kind of generics is being added).

Objective-C/Cocoa uses a single-root style of object oriented framework. Collection classes, therefore, contain pointers to objects. Cocoa has various collection classes and gobs of other useful objects built in.

It's not quite as efficient as templated container classes can be, and it's not as type-safe. On the other hand, Cocoa is a really nice environment to develop in.

Anyway, Objective-C really takes a more dynamic, run-timey approach to objects than C++, which is counter to the static, compile time nature of templates. To get the full benefit of templates in Objective-C you'd have to lose the dynamic nature of objects used with them. And If you keep the full dynamic nature of Objective-C objects, you'd have to lose most of the benefits of templates.

As someone who came from a hard-core C++ background to Objective-C, Cocoa/Objective-C has really won me over. As a developer I think it gives you a lot more than you lose. I think a lot of it has to do with how nice the Cocoa framework is, really.

Soulstorm
Aug 7, 2007, 02:06 PM
FYI, Soulstorm, adts (or ADTs) are "Abstract Data Types" which you've correctly guessed the meaning of.

Objective-C doesn't have templates (I don't have direct knowledge of what's on tap for Objective-C 2.0, but I don't believe any kind of generics is being added).

Objective-C/Cocoa uses a single-root style of object oriented framework. Collection classes, therefore, contain pointers to objects. Cocoa has various collection classes and gobs of other useful objects built in.

It's not quite as efficient as templated container classes can be, and it's not as type-safe. On the other hand, Cocoa is a really nice environment to develop in.

Anyway, Objective-C really takes a more dynamic, run-timey approach to objects than C++, which is counter to the static, compile time nature of templates. To get the full benefit of templates in Objective-C you'd have to lose the dynamic nature of objects used with them. And If you keep the full dynamic nature of Objective-C objects, you'd have to lose most of the benefits of templates.

As someone who came from a hard-core C++ background to Objective-C, Cocoa/Objective-C has really won me over. As a developer I think it gives you a lot more than you lose. I think a lot of it has to do with how nice the Cocoa framework is, really.

I wouldn't have said it better myself.

To all that, I want to add that C++ will never be replaced by ObjC and vise versa.

C++ is for closed world situations (like designing the engine compartment of a car) and ObjC is for open world situations (like designing the interior of the car and its behavior on the road). And, there are some features of OS X that you can't touch without using C++, like some Core Foundation features. I know, those features are also accessible using C, but who uses C when he knows how to use C++?

However, ObjC is really the best choice for OS X development. The ease, power and flexibility that Cocoa provides are unmatched.

hydrorush
Aug 8, 2007, 04:43 PM
on that note then what would be the best book to purchase to teach me the best ways to use the cocoa framework. Im very confident using c++ and obj-c (i read though a rather lengthy tutorial on it already and its obviously very similar to c++) so i'd like a book centered on just the tools and frameworks osx provides to build applications.