What are the limitations of C++ running on the iPhone? - c++

I like C++ a lot and to be honest the Objective-C "super set" 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

Related

Swift and Objective-C++ Interoperability

I have a C++ API with which I need to interact inside a Swift project. I know I cannot interact with C++ from Swift directly, but I know I can do this through an Objective-C wrapper. That much is clear.
However, I cannot find anywhere if Swift and Objective-C++ can interop with each other. I'd like to write the C++ wrapper in Objective-C++, if possible, given the API's complexity. Does anyone know if Objective-C++ and Swift can interop?
Swift and objective-c++ cannot interact. As soon as there is c++ code in the objective-c bridge header that the swift compiler use, you will get a compile error.
You need to completely hide your c++ code behind a pure c or pure objective-c API.
I don't see why they wouldn't, as Objective-C++ is fundamentally Objective-C. You might have to (you want to in any case) hide the C++ object pointers as private properties in the implementation file, but other than that there shouldn't be any issues.
Obviously you'll have to completely extract/abstract the C++ objects.

Objective-C++ Absolute Confusion Over UI; Best Practices

I've been working at iOS and some Mac programming for years now, and have started reading the documentation for c++. It interests me to learn how to use c++ in iPhone apps, but all that I've read assumes that you've jumped the precipice of a learning curve that comes with c++ AND ObjC. So, ill start with the basics.
Let's say I have class A, and class A wants to implement a simple function from .cpp file B. I would then Create a .mm file, so class A could mix objC and c++, but my problem is that I have no idea WHERE the c++ code goes...
In a regular objC method, I would declare methods inside the .h after the #interface line, but with c++, it's just a mess to me...
But then a harder example. What if I want to display UI elements from a C++ class on the iPhone? I have found little to no blog posts or discussions regarding c++ UI (save those lauding the QT library), and nothing related to iOS UI in c++.
Does anybody have code, or best practices for tightly integrating c++ code into an iOS app?
I think you have the wrong impression of what objective-C++ is. Objective-C++ just lets your Objective-C code work with C++ and vice versa. It doesn't bridge the two languages or object models, it only supports integration within the same source file.
For example, when using UI elements you would still need to use objective-C classes. However, those classes could then interact with C++ classes in the same implementation file.
C++ classes are usually defined in headers as well, just like Objective-C classes. Then in the source file you'd implement their methods etc.
I recommend a good C++ introduction to get familiar with C++.
What if I want to display UI elements from a C++ class on the iPhone? I have found little to no blog posts or discussions regarding c++ UI
That is because that, while possible, is simply not necessary. As you can mix ObjC and C++ together in ObjC++ source files, you can just use the usual libraries.

Objective C to C++ conversion [duplicate]

I have no Objective-C experience whatsoever but have a strong C++ background. Is there an automated tool/script or, worst case, some manual method using some excellent reference to port code written in Objective-C to C++? What are the difficulties involved?
Edit:
I'm told the code uses Objective-C fairly trivially. It's an iPhone app that probably doesn't use much in the way of OS-level UI. The C++ version is meant for a non-Apple platform where GNUStep is not an option, so Objective-C++ is not an option.
I worked on same problem. And have some solutions:
Microsoft now offers its own Objective C "bridge", albeit only for UWP apps.
https://github.com/Microsoft/WinObjC
In 2007 I tried to support Objective-C in Visual Studio and wrote my own ObjC runtime, but I do not have more time for writing parser for it. If you have, you can :)
http://code.google.com/p/qobjc/
I wrote the basic functionality of Foundation Framework in C++. In this case, you need to port manually; your code will be on C++ but it will be like on Objective-C. This library worked on iPhone.
http://code.google.com/p/dcocoa/
There are no automated tools that I'm aware of. The dynamic nature of Objective-C is very hard to translate to C++, so quite a bit of brain effort is going to be required for all but trivial Objective-C code. Are you willing to stay on OS X (or keep the GNUStep dependency if you're one of the few people using Objective-C on an OS besides OS X)? If so, the easiest approach is to use Objective-C++ to build a bridge beteen the Objective-C and C++ code. Objective-C++ is an Apple extension to Objective-C and the GCC compiler that allows you to mix Objective-C and C++ code. You can thus create Objective-C objects that call or reference (but not inherit from) C++ objects and you can send Objective-C messages to Objective-C instances from within C++ code.
Objective-C was just a pre-processor (and a runtime library). You'll find more problems in porting all the libraries than the end-user code.
Nil-eating behavior is going to be fun when translating.
You are likely to start seeing bad design in all c++ code after using Objective-C frameworks for a while.

What is the difference between c++, objective-c and objective-c++?

I want to know the difference between c++ and objective-c and objective-c++.
Can any one give me the difference and Can we use the c++ for iPhone development
Thank you,
Madan Mohan
C++ is Bjarne Stroustroup's language based on adding classes and metaprogramming to C in such a way that puts most additional work into the compiler, and relies on least possible effort at runtime.
Objective-C is Brad Cox's language based on adding a SmallTalk-style dynamic message-passing runtime library to C, with a small amount of syntax addition to make it easier to use.
Objective-C++ is, to put it bluntly, what you get when you add the Objective-C runtime and syntax to C++. It has its limitations (e.g. you can't create an Objective-C subclass of a C++ class or vice versa, and Objective-C doesn't like C++ namespaces) but allows you to use C++ classes from Objective-C objects and vice versa.
You can use Objective-C++ in iPhone development. What this means practically is that you could write an application whose object model was entirely C++, where the controller layer would need to interface to Objective-C in order to use the Cocoa Touch APIs.
C++ and Objective C were/are two different approaches to adding object orientation to C. Current objective C compilers also accept C++ as input, so you can build a program with some files written in Objective-C and other files written in C++. When C++ is used this way, it's often called Objective-C++.
1) C++ is a language derived from C that adds Object Orientation (OO) amongst other features. *
2) Objective-C is a language derived from C that adds Object Orientation (OO) amongst other features. *
3) Objective-C++ is Objective-C that you can use C++ classes with.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
(*) Though they both try to solve the same problem they do it quite differently. There is some information about the differences on wikipedia and I'm sure you can use google to find more.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
This will very likely give you code and possibly memory bloat. As you know, iOS programming should be a lean as possible: minimize both the library size and runtime memory needs. iOS programming and runtime environments are also highly optimized for Objective C.
Pure ObjC is much better than C++ for iOS. Unless you're trying to use existing large C++ code base it will probably be better to re-write from scratch. Nearly all of the C++ STL have analogs in the iOS frameworks, often easier to use, and highly optimized by Apple.
Learn Objective C memory management, get familiar with the frameworks and go for it.

What is the best way to port from Objective-C to C++?

I have no Objective-C experience whatsoever but have a strong C++ background. Is there an automated tool/script or, worst case, some manual method using some excellent reference to port code written in Objective-C to C++? What are the difficulties involved?
Edit:
I'm told the code uses Objective-C fairly trivially. It's an iPhone app that probably doesn't use much in the way of OS-level UI. The C++ version is meant for a non-Apple platform where GNUStep is not an option, so Objective-C++ is not an option.
I worked on same problem. And have some solutions:
Microsoft now offers its own Objective C "bridge", albeit only for UWP apps.
https://github.com/Microsoft/WinObjC
In 2007 I tried to support Objective-C in Visual Studio and wrote my own ObjC runtime, but I do not have more time for writing parser for it. If you have, you can :)
http://code.google.com/p/qobjc/
I wrote the basic functionality of Foundation Framework in C++. In this case, you need to port manually; your code will be on C++ but it will be like on Objective-C. This library worked on iPhone.
http://code.google.com/p/dcocoa/
There are no automated tools that I'm aware of. The dynamic nature of Objective-C is very hard to translate to C++, so quite a bit of brain effort is going to be required for all but trivial Objective-C code. Are you willing to stay on OS X (or keep the GNUStep dependency if you're one of the few people using Objective-C on an OS besides OS X)? If so, the easiest approach is to use Objective-C++ to build a bridge beteen the Objective-C and C++ code. Objective-C++ is an Apple extension to Objective-C and the GCC compiler that allows you to mix Objective-C and C++ code. You can thus create Objective-C objects that call or reference (but not inherit from) C++ objects and you can send Objective-C messages to Objective-C instances from within C++ code.
Objective-C was just a pre-processor (and a runtime library). You'll find more problems in porting all the libraries than the end-user code.
Nil-eating behavior is going to be fun when translating.
You are likely to start seeing bad design in all c++ code after using Objective-C frameworks for a while.