Distinct "static" parent property contents for children

Steven Schveighoffer schveiguy at yahoo.com
Wed Nov 8 18:33:15 UTC 2017


On 11/8/17 12:38 PM, Timoses wrote:
> Hey,
> 
> wrapping my head around this atm..
> 

[snip]

so what you want is a static variable per subclass, but that the base 
class can access.

What I would recommend is this:

abstract class Base
{
    int x();
}

class A : Base
{
   private static int _x = 1;
   int x() { return _x; }
}

class B : Base
{
   private static int _x = 2;
   int x() { return _x; }
}

Note that this doesn't do *everything* a static variable can, since x() 
requires an instance (i.e. you can't do A.x). But the actual variable 
itself is static since the backing is static. This is the only way to 
provide access of x to the Base that I can think of.

-Steve


More information about the Digitalmars-d-learn mailing list