Can you make an Objective-C++ subclass of a C++ class

Go To StackoverFlow.com

1

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.

2012-04-04 19:15
by Drew C
Funny, I asked a similar question earlier, with an example of how to do it: http://stackoverflow.com/questions/10014684/objective-c-disadvantages-to-bridging-with- - Richard J. Ross III 2012-04-04 19:18
"Can I make the OS X subclass an Objective-C++ (.mm) file" <- this makes no sense - NoName 2012-04-04 19:28
Please post the line of code causing the error - mydogisbox 2012-04-04 22:08
mdriver = new VideoDriverOSX(mguiinfo, mfifoin) - Drew C 2012-04-04 22:15
What type of file is the call in? .mm or .cpp - mydogisbox 2012-04-04 22:18
It's called from a .cpp file - Drew C 2012-04-04 22:25
That looks like the compiler can't find the implementation file. Make sure you have the correct libraries on your build path - mydogisbox 2012-04-05 13:44


0

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++.

2012-04-04 20:43
by dreamlax
Technically speaking, there are no objective-c classes in objective-c++, they are all objective-c++ classes. More clearly, this means that when you compile objective-c++ the "objective" classes compile to C++ classes, not C structs etc - mydogisbox 2012-04-04 21:04
@mydogisbox: Are you joking? Where on earth did you hear that? You have been grossly misled, otherwise please prove it to me by pointing it out in the Objective-C runtime or clang compiler source - dreamlax 2012-04-04 21:10
@mydogisbox: Objective-C classes do not compile to anything other than Objective-C classes when compiled as Objective-C++, otherwise they wouldn't interop with the existing non-Objective-C++ classes - dreamlax 2012-04-04 21:12
Changing it to a .mm file now lets me use Objective-C in the class, but I'm getting a dynamic linker error when I try to instantiate the object described by the .mm file. Is this fixable - Drew C 2012-04-04 21:42
The explanation is a bit lengthy, but essentially it boils down to objective-c++ being a superset of C++ NOT of C. If you go to the "Objective-C Phrasebook" on amazon, use the look inside feature and search for "C Is Objective-C" it will explain the relationship between objective-c++ and C++ as well as Objective-C and Objective-C++ - mydogisbox 2012-04-04 21:42
@DrewC What exactly are you trying to do - mydogisbox 2012-04-04 21:46
@mydogisbox, See my edit with more details - Drew C 2012-04-04 22:05
@mydogisbox: Yes, that's what I'm saying, but you're purporting that Objective-C classes compiled as Objective-C++ somehow become C++ classes, and this is blatantly and provably false - dreamlax 2012-04-04 22:26
@dreamlax if you want to continue this conversation we should probably take it to chat - mydogisbox 2012-04-04 22:42
@mydogisbox: There's nothing more to discuss. If you want to believe that Objective-C objects can magically turn into C++ classes then that's your prerogative. Meanwhile, in the real world, I'll continue to know that it's not true - dreamlax 2012-04-04 22:58
@dreamlax ok, saying it compiles to C++ is incorrect. Objective-C originally compiled to C, but not anymore and Objective-C++ doesn't compile to C++. This "otherwise they wouldn't interop with the existing non-Objective-C++ classes" is plain wrong, otherwise C++ couldn't interop with C and when in Objective-C++ the only legal language in that file is C++, not C, thus it's Objective-C++ classes, not Objective-C classes because the class is based on C++ not C - mydogisbox 2012-04-05 04:08
@mydogisbox: No, it's still and Objective-C object, even if it uses C++ code in its methods. The Objective-C language definition is "compatible" with the C++ language defintion insofar that a union of both Objective-C and C++ language definitions contains no conflicts. If C++ and C interopped without issue there would be no need for 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
@mydogisbox: You can't use C++ features for Objective-C objects (e.g. operator overloading, namespacing, etc.) and you can't use Objective-C features for C++ objects (e.g. @selector()/performSelector:, runtime subclassing etc.) - dreamlax 2012-04-05 04:55
'All "Objective-C++" means is that you can compile Objective-C and C++ in the same source file.' This is not quite true, and it gets at the point I'm trying to make. Objective-C is not something you can add to C++; it is C with a smalltalk language extension. Likewise Objective-C++ is C++ with a smalltalk language extension. You can't have Objective-C in a C++ file for the same reason you can't have C in a C++ file. The reason the smalltalk language extensions for C and C++ are compatible is because they consist of cross-compilable language features (such as structs) - mydogisbox 2012-04-05 13:41
let us continue this discussion in chatdreamlax 2012-04-05 23:17


0

"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.

2012-04-04 21:04
by mydogisbox


-2

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:

  • that have an instance of the C++ object
  • that implement methods that redirect to the C++ object.

Depending on how simple the API is, this may or may not be an easy task!

2012-04-04 19:33
by joerick
"There's no such thing as an Objective-C++ object." is not correct. In objective-c++ there are no objective-c classes, there are only C++ and objective-c++ classes - mydogisbox 2012-04-04 21:00
I'd disagree. You can use a class that was defined in and Objective-C++ file in plain Objective-C code, and Objective-C has no knowledge of Objective-C++. So that class is an Objective-C class. The fact that in the implementation of this class you can use C++ objects does not affect the interface - joerick 2012-04-05 10:54
Of course you can. Just like you can use C++ structs in a C implementation file (since that's that base of an objective-c class). That doesn't change that a "objective" class defined in C++ code is an extension of C++ not C - mydogisbox 2012-04-05 13:32
Ads