0

I am new to Obj-C, and trying to do the following self-declared C++ operations within header files in Obj-C:

myheader.h

....

struct CINT
{

int  re, im;
CINT (void) : re(0), im(0) {}
CINT (const int data) : re (data), im (0) {}
CINT (const int data_re, const int data_im) : re (data_re), im (data_im) {}
CINT (const CINT& data) : re(data.re), im(data.im) {}
CINT&  operator =   (const int   Op2) { re =  Op2; im = 0; return *this; }
CINT&  operator +=  (const CINT& Op2) { re += Op2.re; im += Op2.im; return *this; }
CINT&  operator -=  (const CINT& Op2) { re -= Op2.re; im -= Op2.im; return *this; }
....
CINT   operator +  (const CINT& Op2) const { return CINT (re + Op2.re, im + Op2.im); }
CINT   operator -  (const CINT& Op2) const { return CINT (re - Op2.re, im - Op2.im); }
CINT   operator *  (const CINT& Op2) const { return CINT (re*Op2.re - im*Op2.im, re*Op2.im + im*Op2.re); }
....

Would it be possible to keep such declarations and operations in obj-C? or need to re-write the whole thing? If so, how? Thanks!

  • Just change the suffix of file including `myheader.h` from m to mm. – KudoCC Oct 21 '14 at 11:22
  • Can you clarify what you're trying to do? That is C++ code. Of course that won't work in ObjC. Do you want to use existing C++ code from an ObjC file? Do you want to know how to write equivalent functionality in ObjC? This may just be a duplicate of http://stackoverflow.com/questions/2226912/can-i-separate-c-main-function-and-classes-from-objective-c-and-or-c-routines/2263291#2263291 – uliwitness Oct 21 '14 at 11:59
  • Thanks for the reply. I am using existing c++ codes to perform lots of arithmatic complex operations. If I re-write the whole thing that would be very time-consuming. I understand that the initialization lists and operator overload won't work in obj-c, so just tried to see if there is any way other than re-writing codes to complie my codes in Xcode. All those were defined in the header file "myOwnComplex.h", can I still keep it in that way? or need to move those definitions to a .mm file? Thanks! – motorcycle999 Oct 22 '14 at 03:12

0 Answers0