C ++在Objective C ++中的用法? [英] Usage of C++ in Objective C++?
问题描述
C ++和Objective C在Objective C ++中有多少集成?我意识到Objective C ++编译的C ++和Objective C代码,但语言实际上是相互交互。
How much integration do C++ and Objective C have in Objective C++? I realize that Objective C++ compiles both C++ and Objective C code, but do the languages actually interact with each other?
例如,我可以创建一个Objective C对象的模板?如何从C ++类继承一个Objective C类?
For example can I create a template of a Objective C object? How about inheriting an Objective C class from a C++ class?
我基本上要问的是做语言组合,还是只是在同一个文件?到什么程度?
推荐答案
使用Objective-C ++构建应用程序是很常见的。你可以将Objective-C包装在C ++代码周围,但是你不能共享对象。例如,你不能在Objective-C中创建一个NSString对象,并尝试从C ++调用hasSuffix方法,反之亦然。因此,为了在这两者之间共享内容,您需要使用C类型,如structs,chars,int等。这是因为Objective-C对象只能在Objective-C运行时环境中使用,而C ++对象只能在C ++运行时环境中使用。所以他们两个共享和可以沟通的唯一的东西是C.
Well it's quite common to use Objective-C++ to build applications. You can wrap Objective-C around C++ code but you can't share objects. For example you can't create an NSString object in Objective-C and trying to call the method hasSuffix: from C++ and visa versa. So to share content between those two you need to use C types like structs, chars, ints etc. It's because the Objective-C objects are only usable in the Objective-C runtime environment and the C++ objects are only usable in the C++ runtime environment. So the only thing they both share and can communicate through is C.
也许一个例子将告诉你如何可以在这两者之间进行交互。
Maybe a little example will show you how you can interact between these two.
NSString * objcStr = [[NSString alloc] initWithString:@"Hello World!"];
std::string cppStr ([objcStr UTF8String]);
std::cout << cppStr;
这篇关于C ++在Objective C ++中的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!