I’ve been experimenting with mixing C++ and Objective-C.
When you port C++ to the iPhone you will probably want to just add your code to an Objective-C project.
Here are some of the issues that may lead to compiler warnings and errors:
- You need to change the extension of your C++ source files from *.cpp to *.mm
- If you reference your C++ *.h file from a *.m file you will get weird syntax errors around your class definition. The reason is because ANY file in your project that references a C++ *.h file must end in *.mm. So if your Objective-C code references a C++ file, change the extension to *.mm.
- You will get warnings that Objective-C ignores constructors and destructors. So you will have to break out that code and call it after defining your object.
- You will get a weird syntax error if you forget to put a semi-colon after your class definition ( class MyClass { … } ; ). This fixes the error “error: new types may not be defined in a return type“. Xcode generates the brackets but doesn’t put the semi-colon in automatically. So be on the lookout for that. You won’t get this error until you add a method that returns something. If you started out with a bunch of void methods – then wonder why it suddenly broke – that’s why.
- The virtual keyword isn’t allowed. In Objective-C all methods are virtual. If your derived class has a matching method, it will be called instead of the one in the base class – as if you did declare it virtual.
See also: Using C++ With Objective-C (developer.apple.com)
Tags: C++