在iPhone应用程序中使用C++ struct

st-*_*t-h 3 c++ iphone constructor struct objective-c

我听说可以在iPhone(Objective C)项目中使用C++ Code,我想使用一个用C++编写的加密库.但是,该库使用的是一个使用构造函数的C++类型结构,我无法做到这一点.

Struct看起来像这样:

struct SBlock
{
 //Constructors
 SBlock(unsigned int l=0, unsigned int r=0) : m_uil(l), m_uir(r) {}
 //Copy Constructor
 SBlock(const SBlock& roBlock) : m_uil(roBlock.m_uil), m_uir(roBlock.m_uir) {}
 SBlock& operator^=(SBlock& b) { m_uil ^= b.m_uil; m_uir ^= b.m_uir; return *this; }
 unsigned int m_uil, m_uir;
};
Run Code Online (Sandbox Code Playgroud)

完整资源来源:http://www.codeproject.com/KB/security/blowfish.aspx

什么是解决这个问题最简单的方法?我已经阅读了关于在苹果开发者网站上使用c ++代码的文章,但这并没有多大帮助.

Pab*_*ruz 6

这绝对是可能的,而且技巧非常简单:当您要在Objective-C++应用程序中使用C++代码时,请将文件命名.mm而不是.m.

所以,如果你有YourViewController.hYourViewController.m,重命名,后者是YourViewController.mm.它将导致XCODE使用C++编译器而不是C编译器与您的Objective-C++代码.

YourViewController.mm:

- (void) yourMessage {
    // will compile just fine and call the appropriate C++ constructor
    SBlock sb(1,1); 
}
Run Code Online (Sandbox Code Playgroud)