Re: private redeclaration of an instance variable
Re: private redeclaration of an instance variable
- Subject: Re: private redeclaration of an instance variable
- From: David Duncan <email@hidden>
- Date: Sat, 29 Jun 2013 19:48:45 -0700
On Jun 29, 2013, at 11:18 AM, Matt Neuburg <email@hidden> wrote:
>
> On Jun 29, 2013, at 10:55 AM, Jens Alfke <email@hidden> wrote:
>
>> This is just a parsing issue. If an ivar is declared in a class’s public interface, it’s in scope in any method of that class or a subclass. So if a subclass declares an ivar with the same name, you now have a conflict and the parser won't know which one you’re referring to, so it won’t let you do that.
>>
>
> That is what I would have thought, but that is exactly what I appear to be doing. That's what I'm finding so odd.
>
> * MyClass, the superclass, defines "thing" as an int, in public (in its interface section in its header file).
>
> * MyClass2, the subclass, defines "thing" as an NSString*, in private (in its implementation section).
>
> I would have expected a conflict. Instead, the compiler seems quite happy, provided any mention of self->thing in MyClass2 is an NSString.
>
> Of course it's possible that I've just confused the heck out of myself and my experiment doesn't show what I think it shows. But try it; I think you'll find that what I'm saying is true. m.
Think of it like scope. Your class's @implementation defines a more limited scope than the class's @interface or its superclass's @interface. When you add a new ivar in the @implementation block, it hides the ivar from the super class, just as what happens when you define a local variable with the same name as an enclosing scope.
What can get confusing is that if you type case to the super class you can still access the hidden ivar. In general I would avoid this (as I would avoid declaring ivars in a public interface).
--
David Duncan
_______________________________________________
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