Re: @protected variable access
Re: @protected variable access
- Subject: Re: @protected variable access
- From: email@hidden
- Date: Mon, 13 Sep 2004 09:27:50 -0700
Tim Hart wrote:
| Assume the following:
|
| @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
|
| I know such access is legal in Java.
Because "protected" in Java means something else entirely: "accessible to subclasses *and* other classes in the same package". It adds "accessible to subclasses" to the not-quite-public access granted by the (default) package access.
| My C++ is a bit rusty, but I'm
| nearly certain it's legal there as well.
No, it's not. In C++, "protected" allows access to otherwise-private variables only for the accessing object itself, not to those of other objects. That is, it allows access only through the "this" pointer.
Glen Fisher
_______________________________________________
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