适用于 iPhone 的 C 与 C++(Objective-C 与 Objective-C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1735085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:03:25  来源:igfitidea点击:

C vs C++ (Objective-C vs Objective-C++) for iPhone

c++iphonecobjective-cobjective-c++

提问by mxg

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

我想为 iPhone 创建一个便携式库,它也可以用于其他平台。

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?

有谁知道在 iPhone 上最好使用什么:Objective-C 或 Objective-C++?它在 C++ 中的工作方式是否与 Objective-C 与 C 的工作方式相同?

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

原因:Objective-C 是 C 的超集,而 Objective-C++is not是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.

UPDATE1:如果有人可以提供更多信息,他会受到欢迎。

采纳答案by Chuck

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.

它们并不是真正不同的语言。Objective-C++ 只是 Objective-C,对包含 C++ 代码的支持稍微有限。Objective-C 是标准方言,但如果您需要使用 C++,没有理由不使用它。AFAIK,最大的实际区别(除了允许使用不同的库)是 Objective-C++ 的编译速度似乎有点慢。如果您决定走那条路,请务必先阅读它,因为 C++ 和 Objective-C 的合并并不是 100% 无缝的。

回答by Stephen Canon

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.

如果您正在编写一个可移植的库,并且不需要特定的语言功能,为什么不直接用 C 编写它呢?它可以很容易地从 C++、C、Objective-C 和许多其他语言中调用。

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 =)

如果您需要 C 中没有的特定语言功能,您应该告诉我们它们是什么,以便您获得更好的建议 =)

回答by Barry Wark

I believe your information is incorrect. Objective-C++ isa superset of C++. Any C++ is legal Objective-C++. In addition, you can mix Objective-C and C++ code in an Objective-C++ (usually .mm) file and (with some restrictions) mix Objective-C and C++ class instance variables within an Objective-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++ sectionof the Objective-C language guide for more info.

我相信你的信息是不正确的。Objective-C++C++的超集。任何 C++ 都是合法的 Objective-C++。此外,您可以在一个 Objective-C++(通常为 .mm)文件中混合使用 Objective-C 和 C++ 代码,并且(有一些限制)在一个 Objective-C++ 类中混合使用 Objective-C 和 C++ 类实例变量。Objective-C++对于Objective-C和C++库之间的接口特别有用。用 C++ 编写您的跨平台库。然后,您可以在应用程序中从 Objective-C++ 调用它。重新阅读Objective-C 语言指南的 Objective-C++部分以获取更多信息。

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

使用 Objective-C++ 的主要缺点是编译时间增加(Objective-C++ 甚至比 C++ 还要糟糕,Clang LLVM 编译器还不能处理 Objective-C++)。除了所调用代码的任何差异之外,Objective-C 和 Objective-C++ 之间没有性能差异(例如,如果您是一个更好的 C++ 开发人员,则您的 C++ 可能比您的 C 库更高效,反之亦然)。

回答by Jonathan Sterling

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.

如果你打算在 iPhone 和 Mac OS 以外的平台上使用这个库,你应该使用 C++。如果您只打算将它移植到 Mac OS,那么继续使用 Objective-C。Objective-C++ 只是将现有 C++ 代码集成到 Objective-C 项目中的一种方式。因此,您不会在其中编写库。但是,您可以使用 Objective-C++ 从 iPhone 或 Mac 项目中使用此库。

回答by TouchGameDev

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.htmlAccessorizer is amazing and cuts development time into nothing.

事实上,你可以在 Xcode 中使用 C 或 C++。将 Objective C 用于 Cocoa 和 Cocoa Touch (iphone) 的原因是因为它易于使用以及它如何处理您自己会遇到的许多内存问题。Objective C 和 Xcode 像梦一样工作。我还建议您查看http://www.kevincallahan.org/software/accessorizer.htmlAccessorizer 非常棒,可以将开发时间缩短到零。

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

话虽如此,我认为用 C 或 C++ 编写函数/方法并在 XCode 项目的一部分中使用它们没有问题。只是不要期望使用 Objective C 在其他平台上也能很好地发挥作用。Apple 和 Windows 开发 IDE 是不同的,Microsoft 有一个很好的习惯,就是不让东西与其他环境(不仅仅是 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

我希望这有帮助。干杯,马修

回答by TouchGameDev

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.

这是您的警告:花一百美元成为开发人员并启动您将在 Mac 上使用的 IDE 的 xcode - 这将是您做出决定的最佳方式,因为很明显您并不完全了解该平台的工作原理根本。

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.

请不要对试图帮助您的每个人无礼。这个社区建立在尊重之上。这里的人真的真的会帮忙和关心,网上很少有这样的地方。

回答by jacai

"Objective-C is a strict superset of C, and Objective-C++ is a strict superset of C++." - https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html

“Objective-C 是 C 的严格超集,而 Objective-C++ 是 C++ 的严格超集。” - https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html

回答by Till

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

选择 C ​​或 C++ - 我看不出有任何理由在不了解您的目标的情况下推荐一个而不是另一个。

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.

在内存使用方面,C++ 和 C 确实需要更严格的编码。在速度方面,C 和 C++ 几乎在任何情况下都比任何 ObjC/ObjC++ 解决方案都快。我确实经常将我的 Objective C 应用程序与 C 和 C++ 代码混合在一起,并且总是对结果感到满意。正如乔纳森正确指出的那样,ObjectiveC++ 主要是在您的 Objective C 应用程序中使用 C++ 代码/库的桥梁。

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.

您可以在 Objective C++ 中自由使用 C++ 对象。但是,您不能在 C++ 中使用 Objective C/C++ 对象。所以它是一条单向路。要克服此限制带来的任何限制,只需在需要的地方使用 Objective C++ 类包装 C++ 对象。