views:

154

answers:

3

Hey guys!

I think I understand how Objective-C++ works in relation to Cocoa, and I am fairly versed in the basics of command-line C++ but sort of afraid to try mixing it with Objective-C. I can create a GUI with Interface Builder in Xcode, and even make classes (.h and .m files) for the interface automatically. The outlets, maybe actions (I still get those confused) leave the curly brackets open to put your own code in. So could I just put C++ code in those brackets to to take input from those buttons (and see the output, etc.)?

I guess if this is the case, one wouldn't have to learn any Obj-C, as long as they knew where to put the C++ code! Am I wrong?

Also, could anyone help me figure that out or at least point me somewhere I can learn more about this? This topic is fairly scarce on the web and I don't understand Apple's documentation.

Thanks for your help.

A: 

Rename .m files to .mm and you will be able to write (Objective-)C++ code in them.

diciu
+3  A: 

My suggestion would be to have Objective-C files in your project (the .m file), then keep your Objective-C++ in separate files (as diciu mentions, .mm files).

One strategy would be use to these Objective-C++ classes simply as wrappers over C++ classes: write your code in C++, in .cpp files like normal. Then, when you need to access some functionality from your C++ from an Objective-C file, write a wrapper Objective-C++ class and call that from your Objective-C.

This last bit feels like a lot of work for small projects, and it is, but if you have a lot of C++ that you need to use, and a comparatively little bit of Objective-C to write, this works pretty well. (Imagine a situation where you are writing a Cocoa interface for a well-factored Windows or Linux program: where all the application logic was written in a platform agnostic manner, and you "just" have to write the UI in Cocoa to get it running on the Mac.

But, another reason for doing this wrapper approach is that Objective-C++ is a hybrid language, so has limitations as to what it's capable of, in both directions.

RyanWilcox
Also, make sure to read _Using C++ With Objective-C_ (http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html#//apple_ref/doc/uid/TP30001163-CH10-SW1), _especially_ the final Limitations paragraph.
Dewayne Christensen
+1  A: 

The thing you need to understand about objective-c++ is that the name is a lie. or at least implies a lot more than you might think.

Objective-c++ does not mean you can write hybrid objective-c++ "objects". It means that you can use c++, and objective c, in the same file. they can reference each other directly - the cannot inherit or derive from each other.

Chris Becke