Re: Help Mixing Objective-C & Objective-C++
Re: Help Mixing Objective-C & Objective-C++
- Subject: Re: Help Mixing Objective-C & Objective-C++
- From: James Bucanek <email@hidden>
- Date: Thu, 24 Feb 2011 08:31:04 -0700
Thanks to every everyone who contributed to this discussion.
In summary...
(1) I agree with Greg that simply omitting the ivar declarations
when compiling in C (i.e. #ifdef __cplusplus/CPPClass*
someCppObj;/#endif) is hazardous. I was thinking that this might
be the most pragmatic solution for problems of limited scope,
but the potential for disaster might be too great.
(2) I think a safer solution is to use preprocessor trickery to
declare C++ class pointers as void* (i.e. CPPClass gets replaced
with void) when compiled in C.
(3) Using an auxiliary structure to contain all of the
implementation variables is a good alternative, but it has other
drawbacks. First, it seriously pollutes all of the
implementation code with pointer redirection through the ivar
struct pointer, which I find both annoying and difficult to
migrate later.
Note that only the first, rather dangerous, technique will let
you declare C++ objects in your Objective-C++ class (i.e.
CPPClass ivarObject;).
(4) Using protocols seems to be the most "correct" solution.
Gwynne's suggestion of using libextobjc to work around the
problem of constructing new objects that have only been defined
only as a protocol is very intriguing. It might be overkill for
this project, but and I'm definitely going to check it out.
(5) Uli's excellent summary also pointed out a hybrid solution
that I hadn't considered: Declare the primary class using
Objective-C, with only C or placeholder C++ ivars (using any of
techniques 1 through 3). A category of that same class could
then be created to implement the C++ methods. Not only can I
then freely mix C and C++ methods in the same class, but the
class can be included in any Objective-C module and used like
any other Objective-C class.
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden