Question

I've been learning C++ for some months now and find it an excellent language albeit its perks.

I was wondering what exactly is the so-called Objective-C++ and if it's worth learning it as a main development language to target Apple environments (ie. Mac OS X, iPhone OS). Searching around the web I only found a couple of good articles: mainly Wikipedia's entry and one from Mac Dev Center.

Wikipedia says:

Objective-C++ is a front-end to the GNU Compiler Collection which can compile source files which use a combination of C++ and Objective-C syntax. Objective-C++ adds to C++ the extensions Objective-C adds to C.

The Mac Dev Center article adds:

Apple’s Objective-C compiler allows you to freely mix C++ and Objective-C code in the same source file. This Objective-C/C++ language hybrid is called Objective-C++. With it you can make use of existing C++ libraries from your Objective-C applications.

The Apple article calls Objective-C++ a language. Is this right?

I would like to know the following:

  • Is it possible to learn and use Objective-C++ without knowing C/Objective-C?
  • Are there any good books, sites, forums, etc where one can get more information and/or help?
  • Are there any big projects done in Objective-C++ as far as you know of?
Was it helpful?

Solution

Yes you should learn Objective-C++ when you want to develop competitive comnerical strength (aka so good that people are willing to pay for them) Apple applications.

It is indeed nothing else the Objective-C and C++ combined in the same file.

For Objective-C use any of the hunderts of the popular apple development blogs/mailing lists/newsgroups. Especially the one directly provided by Apple.

For C++ this is the same, dozens of blogs/mailing lists/starter tutorials are there. But while C and its Objective-C add-on is pretty simple and easy to learn if you already understand programming. C++ is a total different and complex beast. Get a few good books and learn it (after 10 years you will be able to fully understand the language :-) but you can writing C++ programs with just a fraction of this knowledge.

So and now the magic question why you should use C++ when you could get away with Objective-C. The answer is pretty simple. Beside the obvious mentioned wrapping of existing C++ libraries, Objective-C is slow, first of all - method call. THe usual adivse is to do something serious in your methods to avoid this runtime penality that shows up in this language.

But especially for Containers you should really consider to use C++ templates. A vector is much much faster then an NSArray. If your dataset is large you will feel the difference. Also i find C++ containers easier to use because they avoid the typecasts you have to do with Objective-C.

With slow ARM CPU's on iPad and iPhone this is not premature optimization.

Unfortunately you can't mix C++ and Cocoa classes and therefore C++ should only be used for algorithmic data. For the GUI you have to use Cocoa and Objective-C classes.

Getting the right balance between C++ and Objective-C is some part of the skills you need to develop as a Apple programmer.

OTHER TIPS

Objective-C++ is really just mixing Objective-C with C++. Since it allows syntax from both you could argue that it is a new language.

I primarily use ObjC++ (.mm source files) when I have to interface Objective-C code with some C++ library. It is convenient to be able to call C++ in that case. Personally I do not know a lot of people who actually really mix C++ and Objective-C.

1) i doubt it, because everyone of them is a superset of the C language. i don't consider objective-c++ a totally new language. it is just a mix of both that gives you some additional possibilities - i.e. reuse existing c++ code, or use faster c++ in more time critical code sections, or use c++ code where no objective-c / cocoa interface is exposed (mainly hardware related lowlevel stuff like serial ports / ioctls, opengl, ...)

2) i think resources regarding this topics a

3) pass ... i mean what do you consider big. i just know from a bunch of projects (maybe not all of them commercial in the sense that you can buy the software in the store) that use this mix for the reasons listed in 1.

Plenty of large projects use some Objective-C++. Camino, Chromium, and Firefox, for example, are all substantial open-source projects that include Objective-C++.

OBJECTIVE-C++

Objective-C is a small set of extensions to ANSI C. Objective-C++ is the same set of extensions applied to C++. Apple’s Objective-C compiler is also an Objective-C++ compiler.

One of the advantages of Objective-C is that, as a super-set of ANSI C, it can be easily mixed with the millions of lines of existing C code in the world. Objective-C++ can be mixed with the millions of lines of C++ code that already exist. C++ features, such as name mangling, are fully supported by Objective-C++ so that direct linkage between Objective-C++ code and existing C++ code is possible.

Objective-C source code files are identified by the .m extension. Apple’s compiler treats files with the .M or .mm extensions as Objective-C++ source code. Additionally, the –x compiler option can be used to instruct Apple’s compiler to treat any input file as Objective-C++ source code.

Apple’s online documentation describes the features and limitations of Objective-C++ at http://developer.apple.com/techpubs/, and in the release notes that come with Apple’s developer tools. In general, Objective-C classes and C++ classes can be intermixed so that an Objective-C method can call a C++ member function and visa versa or a C++ class can include a pointer to an Objective-C object as a member variable. Objective-C classes cannot inherit from C++ classes or the other way around. The two class hierarchies must remain distinct. The semantics regarding instance creation and deletion are dramatically different between C++ and Objective-C. As a result, mixing them can be tricky, but the benefit of reusing existing C++ code in new Objective-C projects outweighs the complications that it introduces.

Some more practical points about Objective-C++

Gcc is at once a compiler for C, Objective-C, and C++. You can intermix C++ and Objective-C code to some degree. To instruct the compiler that a file contains C++ code as well as Objective-C, use the file extension .mm or .M instead of .m.

Following are some ways in which C++ and Objective-C code can be used together:

  1. Objective-C objects can have fields that point to C++ objects, and vice versa.

  2. Objective-C code can call methods on C++ objects, and vice versa.

  3. Objective-C objects can have C++ objects (as opposed to pointers) as fields, but only if the C++ class has no virtual methods.

However, Objective-C and C++ are not completely compatible. Here are some things you can't do:

  1. Objective-C classes can't inherit from C++ classes, and vice versa.

  2. You can't declare Objective-C classes in C++ namespaces or templates, or vice versa.

  3. You can't use C++ keywords for Objective-C variable names.

  4. You can't call Objective-C methods with C++ syntax, or vice versa.

Finally, there are some restrictions that are imposed to avoid ambiguity:

  1. You can't use the name id as a C++ template name. If you could, the declaration id< TypeName > var could be either a C++ template declaration or an Objective-C declaration using a protocol.

  2. If you are passing a globally-scoped C++ variable to an Objective-C method, you need a space between the first and second colons.

You might find these link helpful :-

  1. https://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1

  2. https://www.raywenderlich.com/62990/introduction-c-ios-developers-part-2

  3. https://www.sitepoint.com/using-c-and-c-in-an-ios-app-with-objective-c/

  4. https://github.com/sitepoint-editors/HelloCpp

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