Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 19, 2016 Struct Inheritence | ||||
---|---|---|---|---|
| ||||
Well struct don't have it, but i would like something like it but only for data, i don't need functions or anything like that just data. struct A { int valueA; } struct B { A a; int valueB; } struct C { B b; int valueC; } C c; c.b.a.valueA; // not elegant B* b = &c.b; b.a.valueA; // we can access A // alternative -------------------------------------------------- struct A { int valueA; } struct B { int valueB; } struct C { A a; B b; int valueC; } C c; c.a.valueA; // a bit more elegant but still not very, c.valueA would be prefered B* b = &c.b; b.? // can't access A, unless we do some hack that assumes B always follows A in the definition Is there any way to do inheritance with structs, is there a reason why we can't extend structures like in C++? |
February 19, 2016 Re: Struct Inheritence | ||||
---|---|---|---|---|
| ||||
Posted in reply to user001 | On Friday, 19 February 2016 at 21:58:24 UTC, user001 wrote:
> Well struct don't have it, but i would like something like it but only for data, i don't need functions or anything like that just data.
>
>
> struct A
> {
> int valueA;
> }
>
> struct B
> {
> A a;
> int valueB;
> }
>
> struct C
> {
> B b;
> int valueC;
> }
>
> C c;
>
> c.b.a.valueA; // not elegant
>
> B* b = &c.b;
>
> b.a.valueA; // we can access A
>
>
> // alternative --------------------------------------------------
>
> struct A
> {
> int valueA;
> }
>
> struct B
> {
> int valueB;
> }
>
> struct C
> {
> A a;
> B b;
> int valueC;
> }
>
> C c;
>
> c.a.valueA; // a bit more elegant but still not very, c.valueA would be prefered
>
> B* b = &c.b;
>
> b.? // can't access A, unless we do some hack that assumes B always follows A in the definition
>
>
> Is there any way to do inheritance with structs, is there a reason why we can't extend structures like in C++?
Indeed, that's very cumbersome, and that's why "alias this" was introduced! It allows you to subtype: calls to members or methods that do not belong the the main type are forwarded to the member. Unfortunately we don't have multiple alias this so you can only forward to one member but it isn't very problematic in practice as otherwise things are likely to become easy to abuse.
Use it like that:
import std.stdio;
struct A
{
int valueA = 1;
}
struct B
{
A a;
alias a this;
int valueB = 2;
}
struct C
{
B b;
alias b this;
int valueC = 3;
}
void main(string[] args) {
C c;
assert(c.valueA == 1);
assert(c.valueB == 2);
assert(c.valueC == 3);
}
|
February 19, 2016 Re: Struct Inheritence | ||||
---|---|---|---|---|
| ||||
Posted in reply to user001 | On Friday, 19 February 2016 at 21:58:24 UTC, user001 wrote:
> Well struct don't have it, but i would like something like it but only for data, i don't need functions or anything like that just data.
>
> [...]
How about
struct A
{
int valueA;
}
struct B
{
A a;
int valueB;
alias a this;
}
struct C
{
B b;
int valueC;
alias b this;
}
?
|
Copyright © 1999-2021 by the D Language Foundation