Is there any way to write a class/overloaded operator so that I could manipulate multidimensional arrays (on the client side) using traditional array[x,y] syntax?
Asked
Active
Viewed 150 times
1
-
2The "traditional" syntax is actually using parentheses e.g. `(x, y)`. That's the syntax MATLAB, FORTRAN, etc. use to index into matrices (which are basically multidimensional arrays). Both of those languages are older than C++. – In silico Nov 16 '13 at 20:25
-
2Could [overload the comma operator](http://stackoverflow.com/q/5602112/1084416) to produce some meaningful object from `x` and `y`. But this is inadvisable (see link). – Peter Wood Nov 16 '13 at 20:28
-
@Insilico an array is accessed via square brackets, i.e. x[0] in C++, is it not? – user997112 Nov 16 '13 at 20:41
-
@user997112: Yes. So? That doesn't change the fact that the syntax for addressing matrices has "traditionally" been via parentheses. It's also how virtually any C++ matrix manipulation library works as well. – In silico Nov 16 '13 at 20:59
-
a[x,y] is not the traditional C/C++ syntax for multidimensional arrays. The traditional syntax is a[x][y], and it is possible to write a class that uses this syntax. It is also possible to use a less traditional a(x,y) syntax. – Frederic Lachasse Nov 16 '13 at 21:11
-
1You wouldn't think that this is would be a frequently asked question! But it is. And [here's the answer](http://www.parashift.com/c++-faq/matrix-subscript-op.html). – Sneftel Nov 16 '13 at 22:51
-
@Ben, if you put that in an answer I'll vote to that. – Jongware Nov 17 '13 at 12:08