Instantiating a class with different types at runtime

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 27 12:57:28 PST 2016


On Sunday, 27 November 2016 at 20:52:06 UTC, Marduk wrote:
> Dear all,
>
> I would like to have a kind of template class like the 
> following:
>
>   class Example {
>
>     this(Type_left x, Type_right y) {
>       this.left = x;
>       this.right = y;
>     }
>
>     Type_left left;
>     Type_right right;
>
>   }
>
> Such that at runtime I can instantiate it with different types:
>
> new Example(int a, int b);
>
> new Example(int a, string b);
>
> I have read about templates and abstract classes, but I have 
> not figured how to get this to work. Thanks.

class Example(L, R)
{
     L _left;
     R _right;

     this(L l, R r)
     {
         _left = l;
         _right = r;
     }
}


More information about the Digitalmars-d-learn mailing list