I have a C++ library (.h only) that contains the implementation of a data structure and I’d like to use it in my iPhone app.
Firstly, I wrote a wrapper in objective-C++ as a class that, through composition, has an ivar of the C++ class. I then was ‘obliged’ to change the wrapper class extension to .mm, and it seemed fine. But then I have to include this wrapped class file into several other files, so I have to change their extension too (in order to prevent a wave of compile-time errors).
Am I correct? Is there a way to ‘confine’ the .mm extension to just few files?(and so preventing name clashes etc)
EDIT: Some more info that might help, I’m using LLVM 1.5 as the compiler (I noticed that the number of compile time errors varies from GCC 4.2 to LLVM 1.5 but I’m not sure that it means much, since I didn’t have a look at them all)
My recommendation is to wrap the C++ bits in #ifdefs:
This is a slightly lame version of the PIMPL idiom, but less code, and effective for hiding C++-isms from your pure Objective-C code. Obvioulsy you would have to include the
ComposedClass‘s header in yourMyWrapper.mm.If
ComposedClassis a templated type, you will need to modify the first block toinstead of using a forward declaration and then, of course, use the templated type in your Objective-C class’ instance variable declaration.
This approach was suggested by Greg Parker, the runtime guru at Apple.