Thursday, May 5, 2011

What Are Some Quirks/Surprises with Using .mm Files in Objective-C?

I want to use some C++ STL collections in my Objective-C iPhone app. Apparently this is possible by giving files the extension ".mm" . What are some of the quirks/surprises associated with this?

I want to use the basic containers that I'm familiar with (vector, queue, set, ...)

Cheers!

From stackoverflow
  • The files will be compiled with the Objective-C++ compiler, so things will behave more like g++ than gcc, but since that's what you want it probably doesn't count as a surprise. From the Objective-C side, I'm not aware of any gotchas, and I've worked on several large projects that made significant used of Objective-C++. Just keep in mind that Objective-C reserved words are reserved in Objective-C++ as well, so you can occasionally run into issues with third-party C++ libraries (e.g., if they use "id" as a parameter in a public interface).

  • You'd be surprised how well it works. I haven't really gotten into any problems intermixing Obj-C and C++ in a .mm file.

  • See Using C++ With Objective-C for a detailed list of what you can and can't do. You can do most things that you would expect. You just can't do things like have a C++ class inherit from an Objective-C class or vice-versa, you can't mix C++ exceptions with Objective-C exceptions, and C++ introduces several new keywords not present in Objective-C.

  • The major quirk of Objective-C++ is that if you don't pass -fobjc-call-cxx-cdtors to g++, it won't call the constructor and destructor of C++ instance variables in of ObjC objects. So remember to turn that option on and you should be good.

    chrish : "On Mac OS X 10.4 and later, if you set the fobjc-call-cxx-cdtors compiler flag, you can use instances of C++ classes containing virtual functions and nontrivial user-defined zero-argument constructors and destructors as instance variables. (The fobjc-call-cxx-cdtors compiler flag is set by default in gcc-4.2.)" -- doc referred to by adam.
    Jean Regisser : Now I understand why my constructors/destructors were being called without this option set in the project when using gcc-4.2 ;-)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.