What are the limitations of C++ running on the iPhone?
I like C++ a lot and to be honest the Objective-C "super set"开发者_C百科 of C is more of a "super fail". Can an iPhone application be written in pure C++? Are there parts of the API that are unavailable from C++?
You can't code purely in C++. For one, the UIApplicationDelegate class every application needs to inherit is Objective-C.
However, nothing is stopping you from coding everything that isn't framework related in Objective-C++. You'll still need to use the Objective-C calls for UIKit and other frameworks, but all of your application logic can be in C++.
From the Objective-C++ section of the Objective-C programming guide, these are the main limitations:
Objective-C++ does not add C++ features to Objective-C classes, nor does it add Objective-C features to C++ classes. For example, you cannot use Objective-C syntax to call a C++ object, you cannot add constructors or destructors to an Objective-C object, and you cannot use the keywords this and self interchangeably. The class hierarchies are separate; a C++ class cannot inherit from an Objective-C class, and an Objective-C class cannot inherit from a C++ class. In addition, multi-language exception handling is not supported. That is, an exception thrown in Objective-C code cannot be caught in C++ code and, conversely, an exception thrown in C++ code cannot be caught in Objective-C code. For more information on exceptions in Objective-C, see “Exception Handling.”
Some Carbon APIs exist in the iPhone so you will be able to access them from within a purely c++ application. That being said, however, there are a lot of very important APIs that are Objective-C only (e.g., UIKit). With some good design decisions a reasonable C/C++ shim could be written to encapsulate the Objective-C necessary to get an iPhone application working. Once that's done you'd be able to write your app with traditional C++ no problem.
The way Objective-C++ is set up you can still write bona fide C++ and mix-in Objective-C calls where necessary. This has been the route I've taken in the iPhone applications I have developed.
I don't think that you'll manage to completely escape objective-c for any meaningful application, but you are free to code mostly in C++. In fact, sio2(one of the iphone game engines) is mostly C++ if you were to use that as the base for your application you could probably avoid writing objective-c yourself.
I would like to know what you don't like about objective-c? I came from a C++ background and find Obj-C to be refreshing and more OO than C++.
You only need to do the GUI in Obj-C (Obj-C++).
There is no problem with integrating any other C++ code with the interface. Just be carefully to manually delete your pointers that are held inside Obj-C objects
精彩评论