C++ to Objective C

Discussion in 'Mac Programming' started by hydrorush, Aug 7, 2007.

  1. hydrorush macrumors newbie

    Aug 7, 2007
    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.
  2. kainjow Moderator emeritus


    Jun 15, 2000
  3. Soulstorm macrumors 68000


    Feb 1, 2005
    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.
  4. iSee macrumors 68040


    Oct 25, 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.
  5. Soulstorm macrumors 68000


    Feb 1, 2005
    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.
  6. hydrorush thread starter macrumors newbie

    Aug 7, 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.

Share This Page