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.

Is there ever a reason to use C++ in a Mac-only application? I not talking about integrating external libraries which are C++, what I mean is using C++ because of any advantages in a particular application. While the UI code must be written in Obj-C, what about logic code?

Because of the dynamic nature of Objective-C, C++ method calls tend to be ever so slightly faster but does this have any effect in any imaginable real life scenario? For example, would it make sense to use C++ over Objective-C for simulating large particle systems where some methods would need to be called over and over in short time?

I can also see some cases where C++ has a more appropriate "feel". For example when doing graphics, it's nice to have vector and matrix types with appropriate operator overloads and methods. This, to me, seems like it would be a bit clunkier to implement in Objective-C. Also, Objective-C objects can never be treated plain old data structures in the same manner as C++ types since Objective-C objects always have an isa-pointer. Wouldn't it make sense to use C++ instead in something like this?

Does anyone have a real life example of a situation where C++ was chosen for some parts of an application? Does Apple use any C++ except for the kernel?

(I don't want to start a flame war here, both languages have their merits and I use both equally though in different applications.)

share|improve this question
4  
Speed and efficiency. –  Glenn Nelson Nov 27 '11 at 16:51
1  
Depends so much on the application being developed. But I never write my code as "Platform Z" only. I assume that one day I will need to port it. Even if that is Apple to Apple. As such I segregate my business logic and UI code completely. UI on mac is obviously Objective-C but business code depends on the application (for me this is usually C++, but that is because I (personally) am more productive in C++). –  Loki Astari Nov 27 '11 at 18:08
    
Since you can mix C++ and Objective-C, you can intermix them in the same project. They each have their strengths - so you can leverage that which makes your life better. –  Mordachai Nov 28 '11 at 19:50
    
@Emil: do the Cocoa framework and the STL count as external libraries? –  outis Nov 29 '11 at 1:49
add comment

3 Answers

  • Familiarity. If you're an expert C++ programmer, you might choose to use C++ for parts of the application that don't need to be Objective-C just because that's what you know best.

  • Platform independence. Even if you're writing a Mac-only application, you might think it wise to avoid tying certain parts of your application to a platform-specific framework. Things change, even on the same platform. For example, if you were asking this question some years ago, you might be working in what is now called Carbon. At some point along the way, you might have wanted to transition to Cocoa. Writing the non-UI guts of your program in a portable manner would make that change easier. C++ would be one good option for that sort of code.

  • Performance. I'd be skeptical if someone told me that they chose C++ because Objective-C's performance didn't meet their needs. Nevertheless, if you have an operation where speed is critical, it might make sense to try implementing in languages other than Obj-C.

share|improve this answer
add comment

If you want pure performance and portability, another option to consider is to code those parts in ANSI C.

Plain C is also a strict subset of the Objective C usually used for coding the OS X UI, so app integration might also be simpler than between code using 2 different OOP semantics.

share|improve this answer
add comment

Developer familiarity is the big one here. If you're on a reasonable project (<= 1 year) and your people know C++ a lot better than Obj-C, you'll be better off doing it in C++. Now if you know (have signed contracts) you'll be doing more Obj-C in the future then it might be worth the spin-up time. This is a business decision, not a technical one.

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.