Stec Records Forum

Tools to make music

News: Note that we've improved the security of the forum by enhancing the sign up form.

Mixing Objective C with C++ (Read 1816 times)

  • Administrator
  • Hero Member
  • Chief Administrator
  • Posts: 771
  • Karma: +0/-0
  • Time reveals truth.
    • Bob Sellon's Home Page
#1 Mixing Objective C with C++
February 18, 2011, 01:01:14 PM
There are still many c/c++ apis required to develop software for Apple devices.  Mixing C++ and Objective C can be tricky so I've started a summary of some basic rules:

1. Any Objective C files that need to work with C++ MUST to use a .mm file name extension which is a trigger for XCode to put different rules in place.  This goes for files that import your class as well.  Unless there is inheritance involved, you can use the @class MyClass declaration in the .h files to avoid problems.  .mm files work like regular .m Objective C files but are more trustworthy of c++ code.

2. Object pointers are incompatible. For instance, to pass an NSString to C++ you need to use [myString cStringUsingEncoding:NSASCIIStringEncoding] to get a "const char *".  This is pretty obvious but even things like 'id' in Objective C, which is similar to a 'void*' in C++ are not compatible. You can't point to a C++ class with an 'id'.