195

What is Objective C++ and can I use this language in Xcode?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Saurabh
  • 22,743
  • 12
  • 84
  • 133

2 Answers2

189

Objective-C++ is simply source code that mixes Objective-C classes and C++ classes (two entirely unrelated entities). Your C++ code will work, just as before, and the resulting executable will be linked with the Objective-C runtime, so your Objective-C classes will work as well. You can definitely use it in Xcode -- name your files with the .mm extension.

Also, you might want to read Apple's (sadly deleted, but archived) documentation on Objective-C++.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Doches
  • 3,276
  • 2
  • 19
  • 26
  • 6
    the limitation section in that link says this and is worth quoting _Objective-C++ does not add C++ features to Objective-C classes, nor does it add Objective-C features to C++ classes. For example, you cannot use Objective-C syntax to call a C++ object, you cannot add constructors or destructors to an Objective-C object, and you cannot use the keywords this and self interchangeably. The class hierarchies are separate; a C++ class cannot inherit from an Objective-C class, and an Objective-C class cannot inherit from a C++ class. In addition, multi-language exception handling is not supported_ – asgs Dec 31 '19 at 08:08
40

Objective-C++ is Objective-C (probably with Cocoa Framework) with the ability to link with C++ code (probable classes).

Yes, you can use this language in Xcode to develop for Mac OS X, iPhone/iPodTouch, iPad. It works very well.

You don't have to do anything weird in your project to use Objective-C++. Just name your Objective-C files with the extension .mm (instead of .m) and you are good to go.

It is my favorite architecture: develop base class library of my game/application in C++ so I can reuse it in other platforms (Windows, Linux) and use Cocoa just for the iPhone/iPad UI specific stuff.

radex
  • 6,336
  • 4
  • 30
  • 39
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292