static data in a sub-class

Jonathan M Davis jmdavisProg at gmx.com
Sat Dec 22 13:03:46 PST 2012


On Saturday, December 22, 2012 21:44:33 Steve D wrote:
> Hello,
> How can I get class B's array printed, using inherited A's
> function? (Dmd 2.060)
> 
>      class A {
>          static float[3] hi = 1;
> 
>          void f() { writefln("hi %s",hi); }
>      }
> 
>      class B : A {
>          static float[3] hi = 2;
>      }
> 
>      B b = new B();
>      b.f();     // prints 'hi [1,1,1]'
>                 // can I get it to use B's overridden hi: [2,2,2] ?
> 
> Thanks for your help :)
> Steve

Functions can be overriden. Variables can't be. So, when hi is used, it'll be 
the one in the class that it's used in unless you specifically specify the one 
to use (e.g. A.hi or B.hi), and simply changing hi to a function won't help 
you, because static functions are never virtual (they have no this pointer, so 
there's no way to look up which version of the function to call). So, if you 
want hi to be overridable, it'll have to be a public or protected member 
function rather than a variable or static function.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list