Private visible?

Dave Dave_member at pathlink.com
Fri Jul 14 08:51:51 PDT 2006


xs0 wrote:
> 
>>> class Number {
>>>     public void setVal(int val) { ... }
>>>     public void setVal(long val) { ... }
>>> }
>>>
>>> class IntNumber : Number {
>>>     public void setVal(int val) { ... }
>>>     private void setVal(long val) { assert(0); }
>>> }
>>>
>>> Number a = new IntNumber();
>>> a.setVal(10L);
>>
>> With 162, it _does_ fail to compile if you move the class definitions 
>> to another module. If the code is all in the same module, then the 
>> assert trips. Both of these seem to work Ok to me.
> 
> Are you sure? I just checked again and the assert doesn't trip, nor does 
> it fail to compile. Here is the exact code I used:
> 
> module main;
> 
> import ba;
> 
> void main()
> {
>     Number a = new IntNumber();
>     a.setVal(10L);
> }
> ==================
> module ba;
> 
> class Number {
>     public void setVal(int val) { }
>     public void setVal(long val) { }
> }
> 
> class IntNumber : Number {
>     public void setVal(int val) { }
>     private void setVal(long val) { assert(0); }
> }
> 

Ahhh, I used 'IntNumber a = new IntNumber();' not 'Number a = new 
IntNumber();' (I didn't copy 'n paste).

But 'a' is a 'Number' and not an 'IntNumber'. D has the same behavior as 
the equivalent C++ in this case.

If I cast: (cast(IntNumber)a).setVal(10L); with your code then I get the 
expected results too.

> 
>>> Reducing visibility should be forbidden, as it doesn't even work and 
>>> leads to bugs.
>>>
>>> Private members should be totally invisible, because that's the point 
>>> of marking them private.
>>
>> IMHO, that may be a little strong...
>>
>> IIRC 'protection' is universally termed "access protection" - not just 
>> in D but when describing OOP protection generically or specifically 
>> for almost any other OOP language. In other words, it specifies 
>> 'access' not 'visibility'.
> 
> Well, I've no idea what term is the exactly right one to use, but I 
> wanted to say that outside their defining module, private stuff should 
> have no effects at all on any other code, as if it wasn't there at all.
> 
> 
> xs0



More information about the Digitalmars-d mailing list