Templated class defaults and inheritence

Joerg Joergonson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 17 19:11:23 PDT 2016


I have something like

class X;
class subfoo : X;
class subbaz : X;

class foo : X
{
     subfoo bar;
}

class baz : X;


which I have modified so that

class subbaz : subfoo;
class baz : foo;

(essentially baz is now a derivation of foo while before it was 
of X)

the problem is that subbaz uses subfoo bar; when it also needs to 
use a derived type. (so it is a full derivation of foo and subfoo)


To accomplish that I parameterized foo so I can do

class foo!T : X
{
     T bar;
}


and I can now do

class baz : foo!subbaz;


There are two problems with this though:


1. How can I create a default foo!(T = subfoo) so I can just 
instantiate classes like new foo() and it is the same as 
foo!subfoo()? I tried creating a class like class foo : 
foo!subfoo; but I get a collision. I guess an alias will work 
here just fine though?(just thought of it)

2. The real problem is that baz isn't really a true derivation of 
foo like it should be. foo!subfoo and foo!subbaz are different 
types. I want the compiler to realize that foo!subbaz(and hence 
baz) is really a derived foo!subfoo and ultimately X.

I'm pretty sure D can do this, just haven't figure out how.

Thanks.












More information about the Digitalmars-d-learn mailing list