Re: @protected variable access
Re: @protected variable access
- Subject: Re: @protected variable access
- From: Marcel Weiher <email@hidden>
- Date: Sun, 12 Sep 2004 11:55:59 +0100
@interface Foo :NSObject { @protected int i; } @end
@interface Bar : Foo
-(id) initWithFoo:(Foo*)foo;
@end
So we have Bar as a subclass of Foo, but also initializable with an
instance of Foo - a quasi-copy initializer.
I was under the impression that initWithFoo could legally do the
following:
@implementation Bar
-(id) initWithFo:(Foo*)foo{
i = foo->i;//legal access to a protected member.
}
@end
Of course it is *not* legal, neither would it be even without
subclassing in a Foo method. You are accessing the property i from the
*outside* here: the property you are trying to access is not part of
self! To be allowed to do that, it would have to be declared @public.
Maybe a bit more explanation of the background is useful here, rather
than just "right" and "wrong". What you are seeing is the difference
between an object orientation (Smalltalk tradition) and abstract data
types (Simula, C++ tradition).
With OO, you have objects and they communicate by messages. That's it.
So two different objects can only communicate by sending messages to
each other. What class or type these objects have is almost completely
unimportant, the encapsulation boundary is based on the object. The
only thing that is important is wether they respond to the messages you
send them.
With ADTs, the type is the unifying principle and therefore also the
encapsulation boundary, so you naturally expect to be able to directly
access to instances of the same type.
(Just for completeness: yup, there are very rare cases when the
accessors might be unacceptable due to the application speed: nearly
always it means you should re-factor your code :) And yup, it does
mean @protected is not really useable in 99.99% of situations and we
should use @private instead. And that means GCC/ObjC, unlike Java/C++,
does it almost right: completely right it would be if @private were
the default.)
No, @protected is absolutely right in the context of OO as discussed
above. The object is the encapsulation boundary, not the class.
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
1d480c25f397c4786386135f8e8938e4
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden