Virtual functions and inheritance

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 27 02:24:26 PST 2015


On Tuesday, 27 January 2015 at 08:19:46 UTC, Daniel Kozák wrote:
> You can use this T:
>
> class Parent {
>     @property string typeName(this T)() {
>         return T.stringof;
>     }
> }
>
> class Child : Parent {
> }
>
> void main() {
>     auto p = new Parent;
>     auto c = new Child;
>
>     assert(p.typeName == "Parent");
>     assert(c.typeName == "Child");
> }

Could 'this T' be used for a static constructor ?

-----
class Bar
{
     static T construct(this T, A...)(A a)
     {
         return new T(a);
     }
}
----

doesn't work. And similarly to the the orginal post:

-----
class Bar
{
     static typeof(this) construct(A...)(A a)
     {
         return new typeof(this)(a);
     }
}

class Foo: Bar{}
Foo foo= Foo.construct; // fail

----

construct() won't be redefined in the Bar descendants.


More information about the Digitalmars-d-learn mailing list