class template as a type

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Sep 19 20:36:14 PDT 2007


"Karl" <asdf at asdf.com> wrote in message 
news:fcsokc$2uhn$1 at digitalmars.com...

> "You could give your template class a non-templated base function, and
> use that as the type of the array elements."
>
> I didn't understood. A small example please.

He meant "non-templated base *class*".  So:

class TestBase
{
    // put common methods here
}

class Test(alias T) : TestBase
{
    this()
    {

    }

    int x;

    ...
}

void Foo(TestBase t) {}
static TestBase[] myArray;

Remember that unlike Java or C# generics, D's templates are done all at 
compile time.  This means that a Test!(int) is just as different from a 
Test!(float) as an int[] is from a float[]. 




More information about the Digitalmars-d-learn mailing list