Struct Inheritance (oopsy alias this)
Koroskin Denis
2korden at gmail.com
Tue Mar 11 13:24:10 PDT 2008
On Tue, 11 Mar 2008 14:02:26 +0300, Sclytrack <Sclytrack at pi.be> wrote:
> Forget what I said, I see that there is already something specified in
> the
> "WalterAndrei.pdf". Something called "alias this" and I forgot about it.
>
> "allows importing the fields of a member into the namespacece of a
> struct."
>
> struct M {int a; }
> struct S {
> M m;
> alias m this;
> int b;
> }
>
> S s;
> b.a;
> s.b;
>
> Wonder if it works with member functions too.
As of now, it's not implemented.
Your best best whould be to use mixins, and it's almost 100% same syntax:
template Base()
{
void doStuff()
{
}
}
struct Derived
{
mixin Base;
}
as opposed to:
struct Base
{
void doStuff()
{
}
}
struct Derived
{
inherit Base base;
}
However, you still can't cast Derived to Base. And proposed syntax (one
with aliased this) most probably won't too (at least, S doesn't look like
it inherits M).
I still think that structs should just support inheritance, maintaining
the limitation of not having virtual functions. Classes could also benefit
from deriving from structs, although this most probably won't ever happen,
since classes are reference types and most importantly contain _vtptr as a
first member and therefore no casting is possible between them.
More information about the Digitalmars-d
mailing list