Thread overview | |||||
---|---|---|---|---|---|
|
July 04, 2007 What does protected mean? | ||||
---|---|---|---|---|
| ||||
I was writing a module with several classes, and found out that classes in the same module can access each other's private methods and even members. Is this by design? If so, then what is protected modifier for? I have read the manual, but didn't understand the difference. |
July 04, 2007 Re: What does protected mean? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | Ald wrote: > I was writing a module with several classes, and found out that classes in the same module can access each other's private methods and even members. > > Is this by design? If so, then what is protected modifier for? I have read the manual, but didn't understand the difference. Yes: Everything in the same module has an implicit "friend" relationship, and they can access each other's private stuff. This is entirely by design, and replaces C++'s explicit "friend" stuff. Otherwise, protected means what it does in many other languages. If something is private, then nothing outside the class (except for things in the same module) can access it, not even subclasses of the class. Protected, on the other hand, is just like private, except it grants access to subclasses. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org |
July 05, 2007 Re: What does protected mean? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kirk McDonald | Kirk McDonald Wrote:
> Ald wrote:
> > I was writing a module with several classes, and found out that classes in the same module can access each other's private methods and even members.
> >
> > Is this by design? If so, then what is protected modifier for? I have read the manual, but didn't understand the difference.
>
> Yes: Everything in the same module has an implicit "friend" relationship, and they can access each other's private stuff. This is entirely by design, and replaces C++'s explicit "friend" stuff.
>
> Otherwise, protected means what it does in many other languages. If something is private, then nothing outside the class (except for things in the same module) can access it, not even subclasses of the class. Protected, on the other hand, is just like private, except it grants access to subclasses.
To be utterly pedantic; it's only relevant for subclasses in another module, because the ones in the same module have 'friend' access.
Regan
|
Copyright © 1999-2021 by the D Language Foundation