Question

I've been learning Objective-C and Cocoa by working my way through the Hillegass book and it occurs to me that I might be better off using Objective-C++. Objective-C seems like the clear choice for developing UIs but I have a very strong C++ background and would love to develop application back-ends in C++ and use Objective-C++ to do the UI integration. But I wonder if Apple will keep developing Objective-C++ or will it become a dead end.

Is anyone out there using Objective-C++?

Was it helpful?

Solution

Disclaimer: I don't work or speak for Apple, so this is my opinion:

I can't speak for the major dev shops, but in my small group, we've used Objective-C++ both for integrating C++ libraries, and as you propose for writing backends in C++. As @alxp mentions, things like exception handling across the language boundary are painful, but with a little planning, most of these pains can be avoided. For experienced C++ devs, the gains can be well worth the pain.

In terms of support, I think you can assume that support in its current state won't go away any time soon. It's part of the GCC code base and the Clang toolchain (Apple's next compiler toolchain) fully supports Objective-C++. On the other hand, there isn't any official guarantee that Apple will continue to develop the integration—fixing some of the warts, for example.

For current projects, I would say that if using Objective-C++ provides benefit, it is safe to rely on the existing support and you should use it.

OTHER TIPS

The only times I've used ObjC++ was to port libraries to make them accessible from my ObjC code. The clashes between how ObjC++ and ObjC handle things like exceptions and class creation and destruction just made it too much of a headache to juggle the two languages in one project.

I don't think support will go away soon as happened with Cocoa / Java since it is pretty solidly part of GCC, and the fact that Objective-C++ compiles down to straight C++ in the same way that Objective-C can compile down to straight C, but I still don't find it a very pleasant environment to build software in compared with Objective-C and being able to comfortably fully use the OS X-provided frameworks.

Objective-C++ is likely to remain supported as long as Objective-C is. Obj-C++ is a basic goal for clang, which is expected to eventually replace gcc as Apple’s preferred compiler. Usage is likely to rise as Carbon applications are moved to Cocoa front ends.

Of course, the word “likely” appears twice above because Apple is so excitingly unpredictable. :-)

I suspect Apple will continue to support Objective C++ for a while, as I don't see any significant recurring effort required by Apple to maintain Objective C++ as Apple updates Cocoa and Objective C++.

The other day I was surprised when I attempted to refactor some Objective-C code that was within an Objective-C++ file using Xcode's refactoring support. Even though the menu items are enabled I got the "Can’t refactor Objective-C++ code. Xcode can only refactor C and Objective-C code." error message. So while compilation of Objective-C++ will continue to function indefinitely I suspect Objective-C++ will be a second class citizen within Xcode.

I took it out of all my code. No .mm files.

But you need .cpp files to talk to .m files. The solution is a .c/.h file that keeps the blood brain barrier intact.

C++ with objective-C is usually just too much baggage.

Objective-C is really just a bunch of C code that emulates objects in the C language, and it still uses the C compiler including the Objective-C header files. Using Objective-C++ uses the C++ compiler and include the Objective-C headers as C code, since C++ will run C code. Objective-C++ is essentially just C, C++, and Objective-C (which is really just C)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top