There are several ways to do this, but in my opinion, the best method is to use the 'PIMPL' idiom , which is quite common in C ++. Make the headers pure Objective-C and pure C ++ with pointers to an expired declaration structure containing the actual implementation. This is defined in the .mm file and then can use Objective-C ++.
In your example, you would do something like this:
// Bh, pure Objective-C: struct BImpl; @interface B { struct BImpl* impl; } // ... // B.mm, mixed: #include "Ch" struct BImpl // since this is C++, it can actually have constructors/destructors { C* my_c; BImpl() : my_c(new C) {} ~BImpl() { delete my_c; my_c = NULL; } }; // make sure to alloc/initialise impl (using new) in B init* methods, // and free it (using delete) in the dealloc method.
I really wrote an article about solving this particular problem, it may seem useful to you: http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects - it also shows some other ways to solve it runtimes, including the original void* approach.
pmdj
source share