Skip navigation.
 
mlRe: Using C++ classes from Objective C
FROM : Clark Cox
DATE : Thu Mar 20 20:13:33 2008

On Thu, Mar 20, 2008 at 11:33 AM, Christopher Nebel <c.<email_removed>> wrote:
> On Mar 20, 2008, at 10:32 AM, 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.
>
>  Actually, you don't need the "void" typedef -- you can exploit the
>  fact that "class" and "struct" are (almost) synonymous in C++:
>
>        struct MyObject; // forward declaration.
>        typedef struct MyObject MyObject; // this is Objective-C, after all...
>
>        @interterface MyObjectWrapper : NSObject
>        {
>                MyObject *myObject;
>        }


Or just forego the typedef altogether:

@interface MyObjectWrapper : NSObject
{
    struct MyObject *myObject;
}

--
Clark S. Cox III
<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