Re: semantics of init
Re: semantics of init
- Subject: Re: semantics of init
- From: Sailor Quasar <email@hidden>
- Date: Tue, 3 Jun 2003 20:19:51 -0400
On Tuesday, June 3, 2003, at 08:02 PM, Niko Matsakis wrote:
For example, suppose I extend SomeClass with SomeOtherClass, should I
do something like this in the constructor?
- (void) init
{
id me = [super init];
me->myfields = some_data;
return me;
}
[snip]
This seems stupid and annoying. I'm assuming that nobody in their
right mind would do anything but return 'self' from init, or if they
did so it would be well-documented and for some no doubt very worthy
cause, so I shouldn't worry about it. I'm wondering, though, if there
is
some nice way to both be safe and not have to write annoying code like
my
first example.
My understanding is that you're supposed to do it like this:
- (id)init
{
self = [super init];
myfields = some_data;
return self;
}
Since when you say "myfields", the compiler translates that into
"self->myfields", the above code works fine. C++ generally frowned upon
assignment to "this", but in ObjC assignment to self is perfectly
normal. However, I don't believe that doing "me->myfields = some_data"
would work in your first example, since "me" was a dynamically typed
id. I don't think, in fact, that you can access fields of an id (or
even a statically typed instance) that way unless you declare a struct
with the @defs() directive and do a specific typecast. ObjC is a lot
more elegant than C++, but the semantics are noticably different.
Disclaimer: I'm relatively new to Objective-C myself, so I may be
completely off my rocker. Someone else on the list should feel free to
correct any bloopers I've made :).
-- Sailor Quasar, High Codemaster of the Web, scourge of systems
cvs server: Updating Quasar/brain/caffiene
A pepsi
R coke
Email: email@hidden
_______________________________________________
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.