简体   繁体   English

可可和Objective-C ++

[英]Cocoa and Objective-C++

I think I understand how Objective-C++ works in relation to Cocoa, and I am fairly versed in the basics of command-line C++ but sort of afraid to try mixing it with Objective-C. 我想我了解Objective-C ++如何与Cocoa结合使用,并且我精通命令行C ++的基础知识,但害怕将其与Objective-C混合使用。 I can create a GUI with Interface Builder in Xcode, and even make classes (.h and .m files) for the interface automatically. 我可以使用Xcode中的Interface Builder创建GUI,甚至可以自动为接口创建类(.h和.m文件)。 The outlets, maybe actions (I still get those confused) leave the curly brackets open to put your own code in. So could I just put C++ code in those brackets to to take input from those buttons (and see the output, etc.)? 出口,也许是动作(我仍然感到困惑)使大括号打开以放置您自己的代码。因此我可以将C ++代码放在这些括号中以从这些按钮获取输入(并查看输出等)。 ?

I guess if this is the case, one wouldn't have to learn any Obj-C, as long as they knew where to put the C++ code! 我想如果是这样的话,只要他们知道将C ++代码放在哪里,就不必学习任何Obj-C! Am I wrong? 我错了吗?

Also, could anyone help me figure that out or at least point me somewhere I can learn more about this? 另外,有人可以帮助我弄清楚这一点,或者至少可以指出我可以在此方面学到更多的地方吗? This topic is fairly scarce on the web and I don't understand Apple's documentation. 该主题在网络上相当稀缺,而且我不了解Apple的文档。

My suggestion would be to have Objective-C files in your project (the .m file), then keep your Objective-C++ in separate files (as diciu mentions, .mm files). 我的建议是在项目中包含Objective-C文件(.m文件),然后将Objective-C ++放在单独的文件中(如diciu所述,.mm文件)。

One strategy would be use to these Objective-C++ classes simply as wrappers over C++ classes: write your code in C++, in .cpp files like normal. 一种方法是将这些Objective-C ++类用作C ++类的包装器:像平常一样,在C ++中以.cpp文件编写代码。 Then, when you need to access some functionality from your C++ from an Objective-C file, write a wrapper Objective-C++ class and call that from your Objective-C. 然后,当您需要从Objective-C文件从C ++访问某些功能时,编写一个包装器Objective-C ++类,然后从Objective-C调用它。

This last bit feels like a lot of work for small projects, and it is, but if you have a lot of C++ that you need to use, and a comparatively little bit of Objective-C to write, this works pretty well. 对于小型项目来说,这最后一点感觉很繁琐,但是确实如此,但是如果您需要使用大量的C ++,并且需要编写相对较少的Objective-C,那么效果很好。 (Imagine a situation where you are writing a Cocoa interface for a well-factored Windows or Linux program: where all the application logic was written in a platform agnostic manner, and you "just" have to write the UI in Cocoa to get it running on the Mac. (想象一下,您正在为一个结构合理的Windows或Linux程序编写Cocoa接口的情况:所有应用程序逻辑都是以与平台无关的方式编写的,而您“只是”必须在Cocoa中编写UI才能使其运行在Mac上。

But, another reason for doing this wrapper approach is that Objective-C++ is a hybrid language, so has limitations as to what it's capable of, in both directions. 但是,采用这种包装方法的另一个原因是,Objective-C ++是一种混合语言,因此双向限制其功能。

The thing you need to understand about objective-c++ is that the name is a lie. 您需要了解的关于Objective-C ++的事情是名称是谎言。 or at least implies a lot more than you might think. 或至少意味着比您想象的要多得多。

Objective-c++ does not mean you can write hybrid objective-c++ "objects". Objective-C ++并不意味着您可以编写混合Objective-C ++“对象”。 It means that you can use c++, and objective c, in the same file. 这意味着您可以在同一文件中使用c ++和目标c。 they can reference each other directly - the cannot inherit or derive from each other. 他们可以直接相互引用-不能相互继承或派生。

将.m文件重命名为.mm,您将可以在其中编写(Objective-)C ++代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM