Mixing C++ with Objective C
Mixing C++ with Objective C
- Subject: Mixing C++ with Objective C
- From: Greg Parker <email@hidden>
- Date: Wed, 3 Aug 2005 11:27:55 -0700
I tried to post this last week. Got no response from it, nor did I
see it appear in the list. If you hav gotten this before, I
apologize... but I urgently need answers to this question.
Your previous message did show up, and you did get several replies.
From the list archive:
http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg02238.html
#import <Cocoa/Cocoa.h>
struct Class0 { void foo(); };
struct Class1 { virtual void foo(); };
struct Class2 { Class2(int i, int j); };
@interface Foo : NSObject {
Class0 class0; // OK <---- Is this OK because there was no
constructor?
Class1 class1; // ERROR!
Class1 *ptr; // OK\227call 'ptr = new Class1()' from Foo' init,
// 'delete ptr' from Foo's dealloc
Class2 class2; // WARNING - constructor not called!
...
@end
OK, but I don't see why Class2's constructor is not called.
Short answer: because the Objective-C runtime doesn't call
constructors like the C++ runtime does.
Long answer:
Before Tiger, the Objective-C runtime never calls constructors for C+
+ objects in Objective-C classes.
In Tiger and later with gcc-4.0, you can compile with the `-fobjc-
call-cxx-cdtors` option. C++ objects in Objective-C classes will be
constructed during +[NSObject alloc], and destructed during -
[NSObject dealloc]. (In other words, your C++ objects are live inside
your -init and -dealloc methods.) Construction uses the C++ class's
no-argument in-place constructor. Note that the constructors still
won't be called if you run on anything older than Tiger.
Is that because it didn't include the arguments... IE:
Class2 class2(i, j); // WOULD this work?
No, there's still no way to do this. You still have to use `Class2
*class2` or call constructors and destructors explicitly if you want
non-default construction.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden