In article <woody-33200E.17164003072008@[EMAIL PROTECTED]
>,
William Woody <woody@[EMAIL PROTECTED]
> wrote:
> > I'm trying to bridge between my Cocoa App and my C++ framework via
> > Objective-C++. Apparently I can't have C++ objects as instance
> > variables in my ObjC class because they contain virtual methods. Also,
> > I don't appear to be able to include my C++ header files in by ObjC
> > header file; they work fine if I include them in my implementation
> > file (.mm).
> >
> > How should I create my C++ objects and use them within my ObjC class
> > (over it's lifetime)? Perhaps I should be doing this a different way?
> > What I have is a C++ framework compiled into a dylib. I include the
> > headers in my .mm and link in the dylib.
>
>
> What I've done is create a pointer in my Objective C class which points
> to my C++ class:
>
> @[EMAIL PROTECTED]
ObjCClass : NSObject
> {
> CPPClass *fPtr;
> }
>
> - (id)init;
> - (void)dealloc;
>
> @[EMAIL PROTECTED]
>
> ...
>
> @[EMAIL PROTECTED]
ObjCClass
>
> - (id)init
> {
> self = [super init];
> if (self) {
> fPtr = new CPPClass;
> }
> return self;
> }
>
> - (void)dealloc
> {
> if (fPtr) delete fPtr;
> [super dealloc];
> }
>
> @[EMAIL PROTECTED]
>
>
> Note that this implies that you either #include the C++ header in your
> Objective C header, or you need to declare the CPPClass in the Objective
> C header as a anonymous C++ class. In either case, this means you can
> only #im****t your Objective C header in a .mm file.
Not So! Remember that in C++ 'struct' is the same as 'class' except in a
'struct' all the members default to public. So, you can forward declare
in your Obj-C.h file:
typedef struct CPPClass CPPClass;
this lets you include your .h file in a .c file just fine.
> There are some places where things can get a little mind-bending, by the
> way; in a few places in my code I've written things like:
>
> std::map<int32_t,MyStruct> array;
> ...
> return array[[myObjC count]];
>
> Where the outer [] is the C++ operator [] call, and the inner [] is an
> Objective C method call.
this is a good place to introduce a tem****ary variable.
Here is a tested example of using the header from a .mm file in a .m:
// main.h
#im****t <Foundation/Foundation.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct CPPClass CPPClass;
@[EMAIL PROTECTED]
ObjCClass : NSObject {
CPPClass *fPtr;
}
- (id)init;
- (void)dealloc;
@[EMAIL PROTECTED]
*MakeObjCClass(void);
#ifdef __cplusplus
}
#endif
//main.mm
#im****t "main.h"
#im****t "cpp.h"
@[EMAIL PROTECTED]
ObjCClass
- (id)init {
self = [super init];
if (self) {
fPtr = new CPPClass;
}
return self;
}
- (void)dealloc {
if (fPtr) delete fPtr;
[super dealloc];
}
@[EMAIL PROTECTED]
main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ObjCClass *obj = MakeObjCClass();
if (obj) {
}
// insert code here...
NSLog(@[EMAIL PROTECTED]
"Hello, World!");
[pool release];
return 0;
}
// cpp.h
struct CPPClass {
public:
CPPClass();
~CPPClass();
private:
int g;
};
// cpp.cpp
#include "cpp.h"
CPPClass::CPPClass() : g(1){
}
CPPClass::~CPPClass() {
}
// m.m
#include "main.h"
ObjCClass *MakeObjCClass(void) {
return [[[ObjCClass alloc] init] autorelease];
}
To summarize:
m.m is straight Obj-C. It has no trouble with main.h.
main.mm is Obj-C++, It has no trouble with the forward declared C++
cl***** in main.h, that are fully declared in cpp.h.
cpp.cpp is straight C++. It only sees cpp.h, so everything is cool.


|