Stec Records Forum

Tools to make music

News: We are back.  We've updated the look of the forum with a new "theme" that works better on mobile devices. Also note that we check all new user requests for spam violations. If you believe you have been rejected unfairly please contact us and we will attempt to resolve it with you.  - Stecrew

Mixing Objective C with C++ (Read 1820 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'.