st-h
st-h

Reputation: 2553

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.

Upvotes: 3

Views: 425

Answers (2)

NSGod
NSGod

Reputation: 22948

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

Upvotes: 3

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

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

Upvotes: 6

Related Questions