开发者

Pure ObjC (.m) vs ObjC & Cpp (.mm) mix

开发者 https://www.devze.com 2023-02-23 00:15 出处:网络
Are there any pitfalls, when mixing Objective C & C++ in a .mm file, should I prefer s开发者_运维知识库ticking to just pure ObjC when possible?Besides the caveats described in the Objective-C++ se

Are there any pitfalls, when mixing Objective C & C++ in a .mm file, should I prefer s开发者_运维知识库ticking to just pure ObjC when possible?


Besides the caveats described in the Objective-C++ section of the Objective-C Programming Guide, there's nothing scary about Objective-C++. It takes a lot longer to compile than Objective-C, however and using C++ in a header infects any code that includes that header with the Objective-C++ requirement. So for your sanity, and the sanity of any clients of your code, use Objective-C when possible in "public" APIs and Objective-C++ when necessary.


Im not sure of any pitfalls that arise from using objective-C++ but I personally would recommend sticking to pure objective-c for clarity and code readability. If you are planning on using some specific C++ libraries then a nice way of going about using objective-C++ in your project would be keep all your C++ functionality in separate C++ class files, that way when you come to using your C++ objects in your .mm files, you can keep mixing up of obc-c and c++ code to a minimum. When I first started IOS dev I wrote a few projects using objective-C++, but the more I came to understand Cocoa and its power, I found the urge to use C++ dwindled as it is better and simpler just to stick with obj-c and the cocoa framework.

0

精彩评论

暂无评论...
验证码 换一张
取 消