www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multi-dimensional array syntax, opIndex

reply Spacen Jasset <spacen yahoo.co.uk> writes:
"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?
Mar 10 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Mar 10 2008