Re: Using C++ classes from Objective C
Re: Using C++ classes from Objective C
- Subject: Re: Using C++ classes from Objective C
- From: "Rob Napier" <email@hidden>
- Date: Thu, 20 Mar 2008 11:11:11 -0400
On Wed, Mar 19, 2008 at 4:12 PM, Jeremy <email@hidden> wrote:
> Hi.
>
> I am just starting to learn Cocoa and would like to use standard C++
> classes from my Objective C/C++ classes.
You really don't. You think you do (probably because you know C++),
but then you enter the crazy messed up world of Objective-C++ and
discover that you really wish you hadn't. A bit of an overstatement,
yes, but really I'd recommend against using ObjC++ unless you have
existing C++ code that you have to bridge to, and then it's best used
just to wrap the C++ classes so that ObjC can deal with them. Learn
Cocoa in pure ObjC.
ObjC and C++ have very different models for managing memory, types,
pointers and well, just about everything. Memory management in
particular is a real hassle. ObjC++ strips away the safety nets from
both ObjC and C++ because now you have to track two very different
ways of managing memory (retain counting versus ref variables for
instance).
> Is there any known documentation on how to do this, or does anyone
> have any pointers?
>
> I tried creating a new object of my C++ class and calling a method on
> it in a .m file and received a bunch of errors (including new not
> being found).
>
> I then changed my .m file to a .mm file and all of the errors went
> away except for one:
> "cannot find interface declaration for 'MyClass'"
This probably means that ObjC expects there to be an ObjC class called
MyClass (rather than a C++ class called MyClass; they're completely
unrelated class structures). I suspect that you accidentally put
"@class MyClass" somewhere rather than "class MyClass", or possibly
wrote "@interface MyClass" somewhere. Alternately, you failed to put
"class MyClass" above the declaration of m_pMemberVariable and so the
compiler assumed MyClass was an ObjC class (this is less likely since
you should have gotten a compiler error elsewhere for that error).
I strongly recommend naming your ObjC and ObjC++ classes differently.
I typically name the ObjC++ classes ending in "Wrapper" but that's
because I only ever use these classes to wrap existing C++ classes
defined in separate C++-only projects.
Also, make sure that you're working in the right namespace. You may
need to say "new myNamespace::MyClass" here.
ObjC++ is deep magic. It is not a good place to learn Cocoa. It is a
useful tool once you know Cocoa and need to integrate it at key points
with C++.
-Rob
--
Rob Napier -- Software and Security Consulting -- http://robnapier.net
"Those who would give up essential liberty to purchase a little
temporary safety, deserve neither liberty nor safety." -- B. Franklin,
Printer
_______________________________________________
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