Private visible?

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Fri Jul 14 06:02:17 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);
> 
> Now, not only does the last line compile, it also calls the wrong
> function and fails to fail.

And casting back to IntNumber makes it again private. No OOP language
should do that. It's against the rules of polymorphism.

> Private members should be totally invisible, because that's the point of
> marking them private.

But then the compiler loses the ability to make intelligent error
messages, right?

> As for overloading issues, as far as I am concerned, feel free to
> require all methods with the same name to have the same protection;
> anything else is poor taste anyway.

Really? Then how do you hide the previous method implementation?

class A {
  protected pm() { ... }
}
class B : A {
  // ugly, isn't it :(
  protected pm() { assert(0); /* subclasses: do not use this */ }
  public pm2() { super.pm(); ... }
}

vs.

class A {
  protected pm() { ... }
}
class B : A {
  public pm() { super.pm(); ... }
}

-- 
Jari-Matti



More information about the Digitalmars-d mailing list