alias this for inheritance

Steven Schveighoffer schveiguy at yahoo.com
Wed Feb 23 07:49:51 PST 2011


On Wed, 23 Feb 2011 10:34:04 -0500, spir <denis.spir at gmail.com> wrote:

> Hello,
>
> I have read several times that alias this is a way to implement  
> inheritance for structs.
> I am simply unable to imagine how to use this feature that way. Has  
> anyone an example?

It allows *some* simulation of inheritance.  However, it does not  
implement polymorphism.

What it does is allow specialization and upcasts.  For example:

struct S
{
    int x;
    void foo() {}
}

struct T
{
    S s;
    void foo2() {};
    int y;
    alias s this;
}

T t;
t.foo(); // translates to t.s.foo();
t.x = 5; // translates to t.s.x = 5;
S s = t; // translates to S s = t.s;

-Steve


More information about the Digitalmars-d-learn mailing list