Re: @protected variable access
Re: @protected variable access
- Subject: Re: @protected variable access
- From: Tim Hart <email@hidden>
- Date: Mon, 13 Sep 2004 14:42:43 -0500
On Sep 13, 2004, at 11:27 AM, email@hidden wrote:
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.
Ah. That was the reason behind the legal access. Thanks for the
clarification.
| 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.
Hmmm... seems my C++ might not only be rusty, but a bit corroded. I'll
have to see about solving that problem.
Thanks for the on topic and philosophy-free response.
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
_______________________________________________
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