Re: Determining instance creator
Re: Determining instance creator
- Subject: Re: Determining instance creator
- From: James Bucanek <email@hidden>
- Date: Fri, 29 Dec 2006 12:21:43 -0700
Michael Watson wrote on Friday, December 29, 2006:
>Let's say I have an instance of AClass, and it creates an instance of
>AnotherClass. During the initialization of AnotherClass, does
>AnotherClass have a way to know what object initialized it?
It depends on what you're asking.
If you want to know what object/class belongs to the code that called the code to create the new object, then no. There's no easy way to determine where any code was called from, and no easy way to correlate that address to a class, and virtually impossible to correlate that to an object.
However, if you are asking if Class C is a subclass of class B, which is subclass of class A, and you are executing -[A init], you can look at isa or [self class] and know if the object is being constructed as an A, B, or C object, and by extension, whether your init code is being called from -[B init] or -[C init].
I've had to use this trick on occasion to create class clusters in a base class that can be subclassed. The -init method looks to see if the client is creating an A object and drops into class cluster mode, where it might delete itself and return some other object. But if the class of the object is B or C, it knows it's just doing the base initialization for a subclass.
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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