Mixing Objective-C and C++

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

  1. macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    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

    Code:
    
    -(IBAction)buttonClicked:(id)sender{
    
    
    //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.
    Sincerely,
    Blake
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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. thread starter macrumors 6502a

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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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).

    -Lee
     
  5. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009

Share This Page