Why is this allowed? Inheritance variable shadowing

a11e99z black80 at bk.ru
Tue Aug 13 06:39:24 UTC 2019


On Tuesday, 13 August 2019 at 05:57:23 UTC, Mike Parker wrote:
> On Tuesday, 13 August 2019 at 04:40:53 UTC, Chris Katko wrote:

> I don't know if I'd call that shadowing. This is how it works 
> in Java, too. There's no such thing as a vtable for member 
> variables -- each class gets its own set and they don't 
> conflict. The only time it could be really be called shadowing 
> is when the base class member is protected, as then it's 
> accessible in the subclass scope.
>
> Also, it's not the same thing as overriding. Overriding means 
> that when you call base.foo(), you get sub.foo()'s 
> implementation. But when you access base.var, you get base.var 
> and not sub.var.
>
> I would find it extremely annoying if it worked the way you're 
> expecting it to.

C# results:
> main.cs(8,14): warning CS0108: `B.x' hides inherited member 
> `A.x'. Use the new keyword if hiding was intended
> main.cs(4,14): (Location of the symbol related to previous 
> warning)
> Compilation succeeded - 1 warning(s)
> mono main.exe
> 1
> 2

with "new" keyword that is used to hide a method, property, 
indexer, or event of the base class into the derived class.
> class B : A {
>   public new int x = 2;
>   // I tell "I want hiding. Ensure "x exists in parent"" 
> explicitly
>   // almost same meaning as "override"
> }

OT:
and again how to easy to google info about error/warning just 
with one word "CS0108"


More information about the Digitalmars-d-learn mailing list