March 10, 2008 Multi-dimensional array syntax, opIndex | ||||
---|---|---|---|---|
| ||||
"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? 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. Instead I should use a function such as: float getIndex(uint row, uint column) ... Is this correct? |
March 10, 2008 Re: Multi-dimensional array syntax, opIndex | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | "Spacen Jasset" <spacen@yahoo.co.uk> wrote in message news:fr4dss$j40$1@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. |
Copyright © 1999-2021 by the D Language Foundation