Re: vtables in objc++
Re: vtables in objc++
- Subject: Re: vtables in objc++
- From: Oscar Morales Vivó <email@hidden>
- Date: Tue, 25 Feb 2003 10:04:23 +0100
He explains it better than I do ;)
What you need to know is that if you place a C++ object as an
Objective-C instance variable, three things will happen:
1) No constructor will be called for the object.
2) The object's data will be zeroed on the parent Objective-C object
creation.
3) No destructor will be called for that object.
Does that mean you can use no C++ objects as Objective-C instance
variables? For the most part, you can't. However, simple structs whose
constructors have no secondary effects and where a zeroed data is a
valid value can be used. For the rest, go with the 'new on init, delete
on dealloc' tactic.
On Tuesday, Feb 25, 2003, at 04:16 Europe/Madrid, Tobias Sargeant wrote:
On Monday, Feb 24, 2003, at 19:30 Australia/Melbourne, Oscar Morales
Vivs wrote:
I can't comment on your error (are you sure using the .mm suffix and
all?) I do have some advice, having done some Objective-C coding.
Remember that Objective-C (and by extension Objective-C++) zeroes all
instance variables when creating an object. That's why you can't use
C++ objects with virtual functions, as the vtable pointer won't get
initialized.
Not quite. zeroing memory first is fine. Not calling constructors
isn't.
Basically, the way obj-c allocates memory means that you can't use C++
objects as
obj-c object members, unless you explicitly call the constructor. This
is because
the obj-c [alloc] message just allocates (zeroed) memory for you, and
doesn't call
constructors. Similarly, [dealloc] won't call any destructors. You
can, however,
use placement new (and an explicit call to the destructor):
@interface Foo
{
std::vector<int> foo;
}
- (id) init;
- (void) dealloc;
@end
@implementation Foo
- (id)
init
{
self = [super init]
new (&foo) std::vector<int>();
}
- (void)
dealloc
{
foo.~vector();
}
@end
i just encountered this:
ld: Undefined symbols:
vtable for CustomControlElement
Often, a missing vtable is results when a pure virtual function is
declared but not implemented. This can happen implicitly with
pure virtual destructors (if you delcare a destructor pure, you
still have to provide an implementation).
Toby.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.