如何在XCode 4.2中一起使用c ++和objective-c [英] How to use c++ and objective-c together in XCode 4.2

查看:208
本文介绍了如何在XCode 4.2中一起使用c ++和objective-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个麻烦,结合c ++和objective-c一起开发一个iphone应用程序。我有一个第三方库在应用程序中使用。我有一个计划使用c或c ++来包装库,然后使用objective-c来调用它。在我用c ++完成类后,我有一个麻烦使用它在objective-c。是否有任何示例代码?感谢。在目标c头文件中

I had a trouble to combine c++ and objective-c together in developing a iphone app. I had a 3rd party library to use in the app. I had a plan to use c or c++ to wrap the library and then use objective-c to call it. After I had finished the class with c++, I had a trouble to use it in objective-c. Is there any sample code? Thanks.

。我写

#import <UIKit/UIKit.h>
#import "ZJTConstants.h"
#include "TTSAdapter.h"

class Adapter;
@interface ZJTVBlogViewController : UIViewController {
@private
    Adapter* adapter;
}
@end

在mm文件中,我写: / p>

and in the mm file, I write:

if (self) {
    adapter = Adapter::getInstance();
    // Custom initialization
}

是否写入?

推荐答案


从Objective-C代码调用C ++代码涉及使用
.mm .m),以便使用Objective-C ++编译器。

Calling C++ code from Objective-C code involves ending your file with .mm (instead of .m) so that the Objective-C++ compiler will be used.

这个编译器可以理解C ++和Objective-C。

This compiler can understand both C++ and Objective-C.

换句话说,ObjC ++编译器允许你直接把C ++代码放在
Objective-C方法中,反之亦然。

In other words, the ObjC++ compiler lets you put C++ code directly in Objective-C methods, and vice versa.

查看 Cocoa_With_Carbon_or_CPP示例使用C ++的策略Objective-C项目(反之亦然)文章。

这篇关于如何在XCode 4.2中一起使用c ++和objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆