Multi-dimensional array syntax, opIndex

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Mar 10 16:12:27 PDT 2008


"Spacen Jasset" <spacen at yahoo.co.uk> wrote in message 
news:fr4dss$j40$1 at digitalmars.com...
> "Multidimensional" array syntax goes like this:
>
> int a[4][4];
>
> a[0][0] = 5;
>
> Which is inline with C, C++. Does anyone know if the syntax [0, 0] was 
> considered for e thlanguage and why was it decided against?

Dunno.  It's still treated as contiguous memory though.

> My second question is if I have a class, Matrix that internally has a 
> float[4][4] inside. I cannot use opIndex to provide an array operator for 
> row and column select.

Yes you can.  :)  In fact this is why the order of the parameters to opIndex 
and opIndexAssign is a little bit odd -- value first -- so that you can 
declare opIndex and opIndexAssign with multiple params.

struct Matrix
{
    float[4][4] data;

    float opIndex(int r, int c)
    {
        return data[r][c];
    }
}

Matrix m;
print(m[2, 3]);

Although I'll agree it's weird that you can do this for your own types, but 
no built-in types support it. 




More information about the Digitalmars-d-learn mailing list