views:

602

answers:

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?

+8  A: 

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
+3  A: 

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
+3  A: 

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
I don't believe this is correct. You can call C++ virtual methods from within Objective-C++ code.
Barry Wark
http://tinyurl.com/yfrxwg9States 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
+1  A: 

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...

ing0
+2  A: 

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
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
A: 

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