www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does protected mean?

reply Ald <aldarri_s yahoo.com> writes:
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.
Jul 04 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
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
Jul 04 2007
parent Regan Heath <regan netmail.co.nz> writes:
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
Jul 05 2007