Skip navigation.
 
mlRe: Using C++ classes from Objective C
FROM : Jeff LaMarche
DATE : Thu Mar 20 21:05:11 2008

What about the UTF8String method? It replaces the old lossyCString 
method and should give you a pointer to a c string

Sent from my iPhone

On Mar 20, 2008, at 3:34 PM, Jeremy <<email_removed>> wrote:

> Thanks everyone!
> Using a void* for the member variable of my wrapper class did the 
> trick and I now have my Objective C code calling my C++ code!
>
> What is the best way to pass strings from my Objective C code to my C
> ++ code though?
> I started looking at the unichar definition and the methods on 
> NSString, but would like to know if there is an obvious standard for 
> dealing with the unicode issues.
>
>
> On Mar 20, 2008, at 1:32 PM, Rob Napier wrote:
>

>> Say you have a C++ object called MyObject in the namespace myapp 
>> that you want to access through your ObjC. What I tend to do is 
>> create an ObjC++ object called MyObjectWrapper that owns a 
>> myapp::MyObject and presents a pure ObjC interface to its methods. 
>> Users of MyObjectWrapper don't have to include MyObject.h. The 
>> trick is that you need to use void* rather than a real class type 
>> in MyObjectWrapper.h like this:
>>
>> typedef void* MyObjectPtr
>>
>> @interterface MyObjectWrapper : NSObject
>> {
>>  MyObjectPtr myObject;
>> }
>>
>> That will get you around having C++ class definitions in your 
>> header. For a large system, hoist Wrapper into a superclass that 
>> can handle setting and releasing its owned object. This is also a 
>> good place to put routines to convert vectors and lists into 
>> NSArrays, and other similar handy routines.
>>
>> I still don't think ObjC++ is the best way to learn Cocoa. As has 
>> been discussed on this list before, the best way to learn Cocoa is 
>> in the language Apple uses and best documents. From there, ObjC++, 
>> RubyCocoa, etc are all useful tools for helping people who already 
>> understand ObjC.
>>
>> -Rob
>>
>> On Thu, Mar 20, 2008 at 12:40 PM, Jeremy <<email_removed>> wrote:
>> Thanks for the input guys, I really appreciate the help.
>>
>> I do have a lot of C++ code that I would like to use and put behind 
>> a Cocoa front end, so I think that using Objective C++ classes as 
>> wrapper classes around my C++ classes is the route I would like to 
>> take.
>>
>> Anyone have an example of such a wrapper class so I can get an idea 
>> of what is the preferred methodology?
>> I'm guessing that you create an Objective C++ class with a member 
>> variable that is a C++ class and then mirror the methods from the C+
>> + class?
>>
>> I'm worried that I'll still end up having to import the header file 
>> for my C++ class in my Objective C source file though, which caused 
>> a lot of compile errors previously.
>>
>>
>> On Mar 20, 2008, at 12:32 PM, John Stiles wrote:

>>> Without starting a religious war, I have to disagree with this.
>>>
>>> ObjC++ is probably a bad idea if you are a novice programmer in 
>>> general, but I think it also has some really good things going for 
>>> it, and having written huge amounts of ObjC++ code, I think it's 
>>> perfectly straightforward to use. It is by far your best bet for 
>>> writing cross-platform code that uses a native Cocoa front end.
>>>
>>>
>>> Rob Napier wrote:

>>>>
>>>> On Wed, Mar 19, 2008 at 4:12 PM, Jeremy <<email_removed>> 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_removed>)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlUsing C++ classes from Objective C Jeremy Mar 19, 21:12
mlRe: Using C++ classes from Objective C John Stiles Mar 19, 21:22
mlRe: Using C++ classes from Objective C Jeremy Mar 19, 21:32
mlRe: Using C++ classes from Objective C Jens Alfke Mar 19, 23:22
mlRe: Using C++ classes from Objective C Rob Napier Mar 20, 16:11
mlRe: Using C++ classes from Objective C John Stiles Mar 20, 17:32
mlRe: Using C++ classes from Objective C Jeremy Mar 20, 17:40
mlRe: Using C++ classes from Objective C John Stiles Mar 20, 18:07
mlRe: Using C++ classes from Objective C Chris Meyer Mar 20, 18:11
mlRe: Using C++ classes from Objective C Rob Napier Mar 20, 18:32
mlRe: Using C++ classes from Objective C Christopher Nebel Mar 20, 19:33
mlRe: Using C++ classes from Objective C Clark Cox Mar 20, 20:13
mlRe: Using C++ classes from Objective C Jeremy Mar 20, 20:34
mlRe: Using C++ classes from Objective C Jeff LaMarche Mar 20, 21:05
mlRe: Using C++ classes from Objective C Rob Napier Mar 20, 21:20
mlRe: Using C++ classes from Objective C Scott Thompson Mar 21, 03:57