A multi-platform app I'm working on uses a different subclass of a single C++ class depending on the platform its running on. Can I make the OS X subclass an Objective-C++ (.mm) file without changing the superclass?
Edit: more details
The project, as it stands now contains this file hierarchy:
* VideoDriver.cpp - (superclass)
- VideoDriver_OSX.cpp - (subclass, contains Mac implementation)
- VideoDriver_win.cpp - (subclass, contains Windows implementation)
- VideoDriver_X11.cpp - (subclass, contains Linux implementation)
In short, I want to be able to use Core Animation and other Cocoa libraries in the VideoDriver_OSX implementation. Changing it to an Objective-C++ file (VideoDriver_OSX.mm) allows me to use these Cocoa libraries, but now the line of code (in a different file) that tries to instantiate the VideoDriver_OSX object causes this dynamic linker error at runtime:
dyld: lazy symbol binding failed: Symbol not found: __ZN15VideoDriver_OSXC1EP10gui_info_sP6CPFifoI17DecodedVideoFrameE
This seems to be related to C++ name mangling, but I don't know how to resolve it. I really appreciate the help, folks.
Renaming a .cpp
file to .mm
will work in a large number of cases, because the Objective-C object system, syntax and runtime are more or less distinct from the C++ object system, syntax and runtime. When you rename a .cpp
to a .mm
, it doesn't magically turn C++ objects into Objective-C objects, they remain as C++ objects. What a .mm
file does is allow you to use both Objective-C and C++ objects in the same file, it is only possible because almost none of the object system, syntax or runtime between Objective-C and C++ clash with each other.
Conversion to Objective-C++ can cause issues if you use variables called new
etc. in your Objective-C code, because this is a keyword in C++. Likewise, any keywords in Objective-C can't be used in C++ code when compiling as Objective-C++.
extern "C" {}
in C++. There's no need for it in Objective-C++ because Objective-C objects remain as Objective-C objects when compiled under Objective-C++. All "Objective-C++" means is that you can compile Objective-C and C++ in the same source file - dreamlax 2012-04-05 04:50
@selector()
/performSelector:
, runtime subclassing etc.) - dreamlax 2012-04-05 04:55
"Can I make the OS X subclass an Objective-C++ (.mm) file without changing the superclass?"
Yes. Assuming that by "subclass an Objective-C++ ... file" you mean that you have a C++ class in your objective-c++ code subclass the C++ class in your .cpp file. If you want to use an objective-c++ class as a subclass of your C++ class, then you're out of luck.
There's no such thing as an Objective-C++ object. In an Objective-C++ file you can either make/use an Objective-C object or a C++ object. Your best bet for bridging is composition- if you're porting a C++ framework to use in Objective-C, create objects:
Depending on how simple the API is, this may or may not be an easy task!