Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 726
st-h Avatar asked Jun 25 '26 19:06

st-h


1 Answers

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); 
}
like image 66
Pablo Santa Cruz Avatar answered Jun 27 '26 09:06

Pablo Santa Cruz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!