FROM : Christopher Nebel
DATE : Thu Mar 20 19:33:51 2008
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;
}
Compiles fine in Objective-C or Objective-C++. In your
implementation, you give the real definition of MyObject:
class MyObject {
...
};
C++ (or at least gcc) doesn't mind that it was forward-declared using
"struct", since the only difference between "class" and "struct" is
the default access privilege.
--Chris N.
DATE : Thu Mar 20 19:33:51 2008
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;
}
Compiles fine in Objective-C or Objective-C++. In your
implementation, you give the real definition of MyObject:
class MyObject {
...
};
C++ (or at least gcc) doesn't mind that it was forward-declared using
"struct", since the only difference between "class" and "struct" is
the default access privilege.
--Chris N.






Cocoa mail archive

