Mixing Objective-C and C++

Discussion in 'Mac Programming' started by Blakeasd, Apr 28, 2011.

  1. Blakeasd macrumors 6502a

    Dec 29, 2009
    I have heard of Objective-C++ and I would like to know if this is possible in Objective-C++. Can I create IBActions that link to buttons and inside these actions I have C++. For example

    //Some C++ code
    Would something like that work. I would like to do this because I physics engine I would like to use requires C++ and I would like to use this engine inside of a nice Cocoa app with all the interface stuff done in Objective-C.
  2. jiminaus macrumors 65816


    Dec 16, 2010
    Assuming the above code is in a .mm file (not a .m file) that should be fine. I don't know about connecting an action directly to C++ method, I don't think that would work. But you should be able to connect it to an objective-c method, and inside that use C++ code, including being able to create C++ object and call C++ methods.

    You know Objective-C is an extension to standard C, right? Objective-C++ is the same extension applied to C++.
  3. Blakeasd thread starter macrumors 6502a

    Dec 29, 2009
    So inside Objective-C actions I can use C++ methods or no?
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Yes, you can. You will need an Objective-C method to receive the action, but it can simply be a thin layer around a call to a C++ method (on, say, a C++ object that is an ivar of your Objective-C class).

  5. Blakeasd thread starter macrumors 6502a

    Dec 29, 2009

Share This Page