Stec Records Forum

Software => Development => Objective C => Topic started by: bsellon on February 18, 2011, 01:01:14 PM

Title: Mixing Objective C with C++
Post by: bsellon on 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'.