6

I want to make an iPhone app, but I am planning to make the framework in C++. Is it possible to use things like templates in Objective-C++. I guess really the question is, can I use boost?

rlbond
  • 65,341
  • 56
  • 178
  • 228
  • This may not be so helpful as an answer but if you already know C++, picking up Objective-C should be a breeze. Unless of course you're porting an app then I understand the question more. – Sean Apr 11 '09 at 23:54
  • It can make sense to use C++ when you need very high performance, like in a game engine. – Chris Lundie Apr 12 '09 at 05:56

6 Answers6

13

Is it possible to use things like templates in Objective-C++.

Yes, but you need to take care how you mix types and interfaces between the pure C++ layers and the Objective-C++ code. Keep in mind the boundaries between layers, where you would need to convert types such as std::string to NSString, and so on.

For example, you could implement the core game engine in pure C++, and just implement your controllers and GUI code in Objective-C++. Then the Obj-C++ code is the glue between the pure C++ engine and Cocoa.

I guess really the question is, can I use boost?

Given the iPhone OS is a subset of OS X that still provides a full POSIX layer, most Boost libraries should work just fine. It should be just like writing Darwin code.

There are a number of limitations in Objective-C++ to be aware of (taken directly from the Objective-C 2.0 Reference Guide):

  • you cannot use Objective-C syntax to call a C++ object
  • you cannot add constructors or destructors to an Objective-C object
  • 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
  • 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.
gavinb
  • 19,278
  • 3
  • 45
  • 60
  • 1
    If you're going to downvote an answer, the least you can do is state why. The accepted answer (currenly with 9 votes) rather over-simplistically states that all features of C++ are available. This answer says a qualified yes, and quotes directly from the language reference explaining the limitations, and it gets downvoted. How does that work? – gavinb Oct 18 '09 at 11:52
  • Limitation #2 heard like general C++ method can be added to Objective-C class. But it's hard to believe adding C++ method to Objective-C class is possible. Can you explain more about this? Because the link is broken I can't check it more. – eonil Apr 09 '11 at 09:47
  • 1
    Unfortunately I can't find an equivalent document to replace the above. I've send a note to the ADC documentation team requesting it be restored. Point #2 simply means that an ObjC class in a `.mm` module still has the same `init` and `dealloc` methods for lifecycle management; you simply can't mix ObjC and C++ approaches within the same class. You can't add a C++ method to and ObjC class, but you can call between the two within a .`mm`. I hope this clarifies the above. – gavinb Apr 22 '11 at 01:45
10

All of C++ is supported in Objective C++. It should be possible to use boost, but you might have to port some of the platform dependant things.

Zifre
  • 26,504
  • 11
  • 85
  • 105
4

It should be pointed out that you can't just do everything that you can do in C++ in Objective-C++. For example you can't call virtual functions on C++ objects from an Objective-C class. Once you call into a C/C++ function you can do whatever you want though.

Caleb Vear
  • 2,637
  • 2
  • 21
  • 20
  • I don't believe this is correct. You can call C++ virtual methods from within Objective-C++ code. – Barry Wark Oct 16 '09 at 21:32
  • http://tinyurl.com/yfrxwg9 States that: "Objective-C++ similarly strives to allow C++ class instances to serve as instance variables. This is possible as long as the C++ class in question (along with all of its superclasses) does not have any virtual member functions defined. If any virtual member functions are present, the C++ class may not serve as an Objective-C instance variable." It appears in newer versions of the OS that this isn't a problem, but I think it is on the iPHONE OS. – Caleb Vear Oct 26 '09 at 05:52
3

Objective C++ is a superset of C++. Everything that you can do in C/C++ can be done in Obj-C++. The "Objective" portion contains, among other things, a Smalltalk-esque messaging system and other additions to C++.

Jason
  • 452
  • 4
  • 9
2

C++ objects in Objective C will NOT necessarily act like in C++. For example constructors and destructors are not automatically called and (i think) that you can't implement virtual methods...

ingh.am
  • 25,981
  • 43
  • 130
  • 177
1

Boost is useful but it is also a large overhead to add to a project.

Make sure you really need it before you go adding it.

For Regex support: RegexLite.

For everything else: Cocoa.

Brock Woolf
  • 46,656
  • 50
  • 121
  • 144