Mixing Objective-C and C++ Mixing Objective-C and C++ objective-c objective-c

Mixing Objective-C and C++


You need to name your .m files .mm. And you will be able to compile C++ code with Objective-C.

So, following your example, your AView.m file should be named AView.mm. It's simple as that. It works very well. I use a lot of std containers (std::vector, std::queue, etc) and legacy C++ code in iPhone projects without any complications.


Never mind, I feel stupid. All you have to do is rename AView.m to AView.mm so the compiler knows it's Objective-C++, and it compiles without a problem.


you could keep the interface cleaner with forward declaration of C++ classes:

#import <AnObjCclass.h>class DBManager; // This is a C++ class. NOTE: not @class@interface AppDelegate : UIResponder <UIApplicationDelegate,                                    CLLocationManagerDelegate,                                    MFMailComposeViewControllerDelegate>{    DBManager* db;...}