views:

2859

answers:

7

I would like to create a portable library for iPhone, that also could be used for other platforms.

My question is the fallowing:

Does anyone knows what is the best to be used on the iPhone: Objective-C or Objective-C++? Does it works with C++ the same way as Objective-C with C or not?

Reasons: Objective-C is a superset of C, but Objective-C++ is not a superset of C++.

Thanks in advance!

UPDATE: What about memory usage, speed in the same implementation of an use case?

UPDATE1: If anyone can provide any more information, he'll be welcome.

+3  A: 

They're not really different languages. Objective-C++ is just Objective-C with slightly limited support for including C++ code. Objective-C is the standard dialect, but if you need to work with C++, there's no reason not to use it. AFAIK, the biggest practical difference (aside from allowing use of different libraries) is that Objective-C++ seems to compile a bit slower. Just be sure to read up on it first if you do decide to go that route, because the merging of C++ and Objective-C is not 100% seamless.

Chuck
+4  A: 

If you are going to use this library on platforms other than the iPhone and Mac OS, you should use C++. If you plan only to port it to Mac OS, then go ahead and use Objective-C. Objective-C++ is just a way to integrate existing C++ code into Objective-C projects. Thus, you would not write a library in it. You might, however, use Objective-C++ to use this library from an iPhone or Mac project.

Jonathan Sterling
+9  A: 

If you're writing a portable library, and don't need specific language features for it, why not write it in straight C? It can be easily called from C++, C, Objective-C, and many, many other languages.

If you require specific language features that aren't available in C, you should tell us what they are so you can get better advice =)

Stephen Canon
+1  A: 

Go for C or C++ - I do not see any reason for recommending one over the other without knowing more about your goals.

In terms of memory usage, C++ and C do demand more disciplined coding. In terms of speed, C and C++ will in virtually any case be faster than any ObjC/ObjC++ solution. I do frequently mix my Objective C applications with C and C++ code and was always pleased with the results. As Jonathan correctly stated, ObjectiveC++ is mostly a bridge for using C++ code/libraries within your Objective C application/s.

You can freely use C++ objects from within Objective C++. You can however NOT use Objective C/C++ objects within C++. So its a one-way-road. To overcome any limitations imposed by this restriction, simply wrap your C++ objects with Objective C++ classes where needed.

Till
I know all of this. As you may know, objc is a superset of C, but objc++ is not a superset of C++. So, it means that depending on the implementation of objc++ may appear some surprises. That's what i'm asking. Are there any surprises?
mxg
Never stumbled upon any hidden caveats.
Till
+8  A: 

I believe your information is incorrect. Objective-C++ is a superset of C++. Any C++ is legal Objective-C++. In addition, you can mix Objective-C and C++ code in a an Objective-C++ (usually .mm) file and (with some restrictions) mix Objective-C and C++ class instance variables within an Objetive-C++ class. Objective-C++ is particularly useful for interfacing between Objective-C and a C++ library. Write your cross-platform library in C++. You can then call it from Objective-C++ within an application. Re-read the Objective-C++ section of the Objective-C language guide for more info.

The major downside to using Objective-C++ is increased compile times (Objective-C++ is even worse than C++ and the Clang LLVM compiler doesn't handle Objective-C++ yet). There is no performance difference between Objective-C and Objective-C++ beyond any differences in the code that's called (e.g. if you're a better C++ dev., your C++ will probably be more efficient than your C library and visa versa).

Barry Wark
By mistake i voted up, but Objective-C++ is a not superset of C++. Please read more careful. Theres the documentation form the link you gave me."Apple’s Objective-C compiler allows you to freely mix C++ and Objective-C code in the same source file. This Objective-C/C++ language hybrid is called Objective-C++."
mxg
As I said in my post, I believe any valid C++ is valid Objective-C++ but not all valid Objective-C++ is valid C++. Thus, Objective-C++ is a (strict) superset of C++, just as Objective-C is a (strict) superset of C. I think you are misinterpreting the text you cite.
Barry Wark
See this answer: http://stackoverflow.com/questions/525609/use-c-with-cocoa-instead-of-objective-c/529017#529017
Barry Wark
And this page from Apple's developer documentation: https://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html#//apple_ref/doc/uid/TP40001670
Barry Wark
+1  A: 

The truth is you can use C or C++ in Xcode. The reason to use Objective C for Cocoa and Cocoa Touch (iphone) is because of it's ease of use and how it handles many of the memory issues you would come across on it's own. Objective C and Xcode work like a dream. I would also recommend checking out http://www.kevincallahan.org/software/accessorizer.html Accessorizer is amazing and cuts development time into nothing.

Having said that I see no problem in writing functions/methods in C or C++ and using them within a part of your XCode project. Just do not expect to use Objective C to play as nicely on other platforms. Apple and Windows development IDE's are different and Microsoft has a nice habit for not making things as compatible with other environments (not just Apple.)

I am glad to hear someone out their trying to reuse their code for other projects : ) Too many people forget to create their own libraries these days and I haven't the slightest idea why.

I hope this helps. Cheers, Matthew

TouchGameDev
+2  A: 

Here is your caveat: Spend the hundred on becoming a developer and launch xcode the IDE you will be working with on the Mac - this would be the best way for you to decide as it is apparent that you do not fully understand how this platform works at all.

Please don't be rude to everyone trying to help you. This community is built on respect. The people here really actually do help out and care and there are few places like this on the net left.

TouchGameDev
;) Matthew, my mistake!
mxg