Distinct "static" parent property contents for children
Timoses
timosesu at gmail.com
Thu Nov 9 12:34:50 UTC 2017
On Wednesday, 8 November 2017 at 18:33:15 UTC, Steven
Schveighoffer wrote:
> 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
I suppose this is what Adam suggested, correct?
This is a more general question: Why is it not possible to
implement/override static methods?
interface I // or abstract class I
{
static int fun();
}
class A : I
{
static int fun() { return 3; }
}
void main()
{
I i = new A();
i.fun(); // <--- linker error
}
or replacing interface with an abstract base class.
Static overriding would be quite interesting in terms of
compile-time handling of objects.
More information about the Digitalmars-d-learn
mailing list