Use class template as a type

dm via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 30 19:19:23 PST 2016


On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote:
> To avoid having to use the Object class directly you can make 
> an base class of the class template.
> Like:
>
> ```
> abstract class MyClass {}
> abstract class MyClassImpl(T)
> {
> public:
>     @property const(T) value(){return _value;}
>     @property void value(T val){_value = val;}
>  ...
>    private:
>     T _value;
>  ...
> }
>
> MyClassInt and float inherits from MyClassImpl
> ```
>
> And use it like:
>
> ```
> void main() {
>    MyClass[] objs;
>    objs ~= new MyClassFloat();
>    objs ~= new MyClassInt();
> }
> ```

Yes, but anyway you need to downcast if(MyClassBlahBlah subclass 
= cast(MyClassBlahBlah)obj)...
So it's not much sense to have base class or interface MyClass.


More information about the Digitalmars-d-learn mailing list