how to change attribute in descendant?

Pragma ericanderton at yahoo.removeme.com
Thu Jan 18 08:32:46 PST 2007


Jari-Matti Mäkelä wrote:
> Pragma wrote:
>> This example also demonstrates D's property syntax, which is a nice
>> concession for situations like this.  Try using this snippet with the
>> "void main()" snippet above. :)
> 
> Eric, do you happen to know, if it's possible to do the opposite - i.e.
> make public methods private by using private/protected inheritance? The
> spec says (http://www.digitalmars.com/d/class.html):
> 
> InterfaceClass:
>         Identifier
>         Protection Identifier
> 
> Protection:
>         private
>         package
>         public
>         export
> 
> But what does it mean? I've asked this a few times before (nobody
> answered). The compiler just bypasses the protection attributes. I think
> other than public inheritance is anyway bad in D, especially when the
> protection is used with interfaces.

(I'm assuming that you're coming from a novice background here - my apologies if this is not the case)

Sadly the documentation is kind of in an "expert mode" state at the moment.  In many places, it assumes that you're 
already familiar with C++ or Java - most of us here are, so we mentally fill in these gaps without realizing they're 
missing!

*ahem* anyway, the protection attributes break down like so:

public - accessible by all
protected - accessible by the declaring scope and any scopes that inherit it
private - only accessible by the declaring scope
package - accessible by all, *within the same package*
export - accessible by external programs and libraries (it's of out of place in this discussion)

...where a scope is a module, interface, class or struct.

Well one thing that isn't illustrated in the grammar (quoted above) is that 'public' is implied for anything accessed 
from within the same module - perhaps this is what you're seeing when the compiler 'bypasses' things?  You have to break 
your classes and interfaces out into distinct files for these attributes to be enforced as you'd expect.

While I know this seems odd, it's much better than what Java or C++ would have to do to achieve the same effect.  From 
the perspective of someone (me) who writes libraries in D, it's a help and not a hindrance.

-- 
- EricAnderton at yahoo


More information about the Digitalmars-d-learn mailing list