struct aliases

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Nov 14 13:50:03 PST 2007


"Kenny B" <funisher at gmail.com> wrote in message 
news:fhfkvd$3fc$1 at digitalmars.com...
> Ok, I have reduced my code to a simple example... This is what I have:
>
> class MyClass {
> struct Data {
> int val1;
> int val2;
> }
>
> Data data;
> void one_function() {
> // stuff
> }
> }
>
> MyClass c = new MyClass;
> // I want to say this:
> c.val1 = 5;
>
> // not this:
> c.data.val1 = 5;

It seems like it's almost possible:

class C
{
    struct Data
    {
        int x, y;
    }

    Data data;

    alias data.x x;
    alias data.y y;
}

void main()
{
    C c = new C();
    c.x = 5;
    Stdout.formatln("{}", c.data.x); // error
}

The error it gives is "this for x needs to be type Data, not type C".  I'm 
surprised that the aliases even compile, though.  I guess they're not really 
expressions in the normal sense.

You could also try making Data an anonymous struct:

class C
{
    struct
    {
        int x, y
    }
}

And now those will be accessible through C references, but now you no longer 
have the Data type and can no longer access both those members as a single 
item.. 




More information about the Digitalmars-d-learn mailing list