Re: Zeroing out instance variables
Re: Zeroing out instance variables
- Subject: Re: Zeroing out instance variables
- From: Ken Thomases <email@hidden>
- Date: Sat, 17 Apr 2010 08:49:33 -0500
On Apr 16, 2010, at 5:34 PM, Paul Sanders wrote:
> Another way might be to provide a method in each class or subclass called, say, zeroIVars and code it as (typed in mail):
>
> - (void) zeroIVars
> {
> static ThisClass *clean_instance_of_this_class;
>
> assert ([self isMemberOfClass: [ThisClass class]);
> if (clean_instance_of_this_class == nil)
> clean_instance_of_this_class = [[ThisClass alloc] init];
> *self = *clean_instance_of_this_class;
> }
To be clear (for the archives), the above byte-wise copy of the object is only safe if the class has no pointers with ownership semantics among its instance variables. See the discussion of object copying in the Memory Management Guide.
> This approach gives you the opportunity to end up with an object that is not just all zeroes (by way of the init method)
The init method has nothing to do with zeroing out instance variables. Object allocation is responsible for that.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden