Can I set the base class like this?

Jack jckj33 at gmail.com
Tue Jan 26 04:39:07 UTC 2021


Can I pass the base class type thought template parameter? 
something like this:

class MyType { }

class A(T)
{
     void doSomething() { }
}

class B(T)
{
     void doSomething() { }
}

// this class shared stuff to deal with A and B
class C(T) : T!MyType
{
     void doSOmethingElse() { }

     override void doSomething()
     {
         doSOmethingElse();
         super.doSomething();
     }
}

then do something like this:

alias Foo = C!A;
alias Baa = C!B;

instead of:

class Foo : A!MyType
{
     void doSOmethingElse() { }

     override void doSomething()
     {
         doSOmethingElse();
         super.doSomething();
     }
}

class Foo : B!MyType
{
     void doSOmethingElse() { }

     override void doSomething()
     {
         doSOmethingElse();
         super.doSomething();
     }
}

note the body is the same, what changes is the base class. I'd 
like to avoid repeating myself when the body is the same and only 
the base class changes.





More information about the Digitalmars-d-learn mailing list