Distinct "static" parent property contents for children

Timoses timosesu at gmail.com
Wed Nov 8 17:38:27 UTC 2017


Hey,

wrapping my head around this atm..

How would I achieve that children statically set a property of 
the parent so that the property is distinct between different 
children?

The following is *bs*, but kind of illustrates what I'd "like":

class Base
{
     int x;
}

class A : Base
{
     static this() { x = 1; }
}

class B : Base
{
     static this() { x = 2; }
}


The dilemma lies in 2 options and their drawbacks:

Option 1:
class Base { }
class A : Base
{
     static int x = 1;
}
class B : Base
{
     static int x = 2;
}

Con: In the above Base does not have access to "x"....


Option 2:
class Base { int x;}
class A : Base
{
     this() { x = 1; }
}
class B : Base
{
     this() { x = 2; }
}

Con: Well, the downside is that every instance of A and B 
allocates space for x although only one allocation would be 
required..


I have found the following propsition: 
https://stackoverflow.com/questions/11516066/d-inheriting-static-variables-differentiating-by-class
However, that would remove the hierarchy..

Are there better options/ways of achieving this?


More information about the Digitalmars-d-learn mailing list