Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 7, 2007, 04:07 AM   #1
hydrorush
macrumors newbie
 
Join Date: Aug 2007
C++ to Objective C

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.
hydrorush is offline   0 Reply With Quote
Old Aug 7, 2007, 08:10 AM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Not sure exactly what you mean, but you can use C++ directly within Objective-C:
http://developer.apple.com/documenta...ection_10.html
kainjow is offline   0 Reply With Quote
Old Aug 7, 2007, 11:33 AM   #3
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by hydrorush View Post
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.
Soulstorm is offline   0 Reply With Quote
Old Aug 7, 2007, 01:16 PM   #4
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
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.
iSee is offline   0 Reply With Quote
Old Aug 7, 2007, 02:06 PM   #5
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by iSee View Post
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.
Soulstorm is offline   0 Reply With Quote
Old Aug 8, 2007, 04:43 PM   #6
hydrorush
Thread Starter
macrumors newbie
 
Join Date: Aug 2007
cocoa book

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.
hydrorush is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
I Need Help Learning Objective-c!! Woodshark69 Mac Programming 11 Dec 14, 2012 07:21 AM
Objective C Accessors ag2web Mac Programming 9 Nov 18, 2012 02:53 PM
Problems with Objective-C++? frank100 Mac Programming 5 Aug 8, 2012 04:49 PM
Testing objective-c Evre iPhone/iPad Programming 21 Jul 12, 2012 07:22 AM
Objective C Syntax nashyo iPhone/iPad Programming 6 Jul 4, 2012 11:56 AM

Forum Jump

All times are GMT -5. The time now is 09:24 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC