简体   繁体   中英

Using C++ struct with iPhone app

As I heard it is possible to use C++ Code within an iPhone (Objective C) project, I want to use an encryption library which is written in C++. However, the library uses a C++ type struct which uses a constructor, that I can't get right.

The Struct looks like this:

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;
};

full source is available here: http://www.codeproject.com/KB/security/blowfish.aspx

what's the easiest way to get around that issue? I've read the article about using c++ code on apple's developer site, but that didn't help much.

It's definitely possible and the trick is extremely simple : when you are going to use C++ code in your Objective-C++ applications, name your files .mm instead of .m .

So if you have YourViewController.h and YourViewController.m , rename the latter to be YourViewController.mm . It will cause XCODE to use C++ compiler instead of C compiler with your Objective-C++ code.

YourViewController.mm:

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

Just change the filename extension of your .m file to .mm and include the C++ headers. Wow, I type too slow, lol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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