Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

For an upcoming XCode iOS project I'm planning on writing my views and view controllers in Objective-C. My model will be provided to me in C++.

I am aware of 2 ways of using C++ in an objective-c project. They are described below. What are the pros and cons of each method. Is there any other recommended way, and what are its pros and cons?

-In C++ header files, use extern "C" declaration as described in Wikipedia.

-Wrapping C++ with an Objective-C++ as described by Rob Napier.

share|improve this question
    
You might find this pdf From C++ to Objective-C useful. –  pcunite Aug 23 '12 at 22:33
1  
For those interested, here is a small demo project I created using the Objective-C++ wrapper method. github.com/boralben/ObjectiveCPPWrapperEx –  Ben Boral Aug 26 '12 at 6:31
add comment

2 Answers

up vote 4 down vote accepted

Your views and view controllers will need to be Objective-C, because of the frameworks. This much is given. The question then is where to put the bridge between your C++ code and your Objective-C code.

Firstly, I'd advise against wrapping the C++ code in C functions with an extern C {} linker convention. You've then given yourself two problems: how to handle the C++ classes and types from within C, and how to use the structured C interface from within Objective-C objects. Both of these problems are surmountable but you may as well not have to introduce them.

Three approaches seem possible:

  • write your application's logic and behaviour in C++, where certain of the classes have member fields that are Objective-C types so that you can interface with the frameworks. This approach is likely to run into "impedance mismatch" problems between the frameworks - which have a "don't call us, we'll call you" policy - and your own code which is trying to take control of the application's behaviour from outside the framework's ambit.

  • in your view controllers, keep references to C++ types as instance variables. Your view controller classes will be Objective-C++ files.

  • write Adapter or Façade classes in Objective-C++ that provide an Objective-C API and internally manage the C++ types. Your view controllers access the model exclusively via this layer.

Of these approaches, I would prefer the third. You separate the responsibilities of "managing the model" (in the C++ code), "displaying model content" (in the ObjC view controller classes) and "bridging C++ to ObjC" (in the adapter layer). You'll meet many developers who know ObjC but not C++ and vice versa, this approach makes it easiest for those developers to contribute to your codebase, whether they end up working with you or just trying to help you out on the web.

share|improve this answer
add comment

I don't know much about Objective-C, but the extern "C" approach will only work if your C++ header files don't contain C++ classes, only C style structs and functions. If your model is written in C++, I guess this will not be the case, so I think you will have to try the second road.

extern "C" is intended to use C code in C++, not vice versa.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.