1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

XCode - C++ with obj-C examples??

Discussion in 'Mac Programming' started by Oats, May 22, 2006.

  1. macrumors regular

    #1
    Hi,
    I am looking to develop an applicate for OS X with Apple's XCode. I have a few classes that I have defined for Visual C++, but they should be fairly cross-platform. So I would like to use those C++ classes, but my impression is that the GUI builder and everything else is built to use obj-C. Are there any examples of mixing these two languages into the same app?
     
  2. macrumors 603

    gekko513

    #2
    I believe you can start a Carbon project to use C++. Carbon has its own set of GUI widgets in Interface Builder.

    If you want to use Cocoa that has more features and integration when it comes to Interface Builder, I think you're limited to Objective-C and plain C.

    As far as I can see, you have three choices:
    1. Make your application using the Carbon framework
    2. Rewrite your classes in Objective C. (It shouldn't be that hard)
    3. Write some sort of bridge in plain C to access functionality in your C++ classes.

    Maybe someone else can offer better solutions...
     
  3. macrumors 68030

    Catfish_Man

    #3
    Fixed :)
     
  4. macrumors 603

    gekko513

    #4
    That's cool. I didn't know you could do that.
     
  5. macrumors 68030

    Catfish_Man

    #5
    It required quite a bit of compiler work on Apple's part. iirc the official version of GCC either just gained objc++ support, or will be soon.
     
  6. macrumors regular

    #6
    so thats sounds pretty easy... there's nothing more to it? just rename my .cpp files to .mm?

    i will also have to learn how to call my c++ classes with obj-c, but that should be pretty straightforward???
     
  7. macrumors 68000

    GeeYouEye

    #7
    Your C++ classes are just that. You shouldn't need to mess with them if they're model (business) objects. Just #include the headers in your Obj-C++ .mm files, (and to the .h if a C++ instance is a data member). Then just instantiate and use as normal. The runtimes are totally separate... it's just a code-level thing; don't pass a C++ string where an NSString * is needed.
     
  8. macrumors regular

    #8
    What the heck is Obj-C++??? Are those files which are a hybrid of Obj-C and C++?
     
  9. macrumors 603

    gekko513

    #9
    Syntactically, Obj-C is in essence regular C with a particular extension for object oriented programming. You can program regular C in Obj-C.

    I'm pretty sure that Obj-C++ is regular C with the C++ and the Objective-C extensions. Thus Obj-C lets you do object oriented programming in two different ways.
     
  10. macrumors 68000

    Soulstorm

    #10
    If you want to program fot OS X proffessionally, you will choose among the Carbon and Cocoa API. The Carbon API is accessible through plain C and C++ and the Cocoa API is accessible through Java, Objective-C and Applescript. The greatest flexibility is offered by Objective-C.

    I tried the Carbon API, but I disliked it. It required much effort just to make a simple program that with Cocoa could do in a matter of minutes. Objective-C offers you many more capabilities at a smaller amount of time. You can still make a proffessional quality program with Carbon, though.

    Objective C++ is a language frontend to the Objectiive-C language. It allows integration of of Objective-C and C++ in the same files, with some limitations considering the inheritance of the classes. Obj-C++ is Apple's thing, though. It doesn't exist on GCC for windows, if I'm not mistaken. I don't know if it will eventually be supported in Windows, but it is a very good solution for Mac programmers who want to take advantage of Objective-C's productivity and Cocoa development with the flexibility and low-level power that C++ gives.
     

Share This Page