Re: About matrix subscripting index
Kai-Uwe Bux a =E9crit :
James Kanze wrote:
On 29 mar, 07:11, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
d major wrote:
I want to implement a Matrix class for numeric computing,
just like this:
class myMatrix{
public:
myMatrix(int m,int n);
...
private:
size_t row,col;
double **mat;
};
So, if I instantiate a matrix
myMatrix T;
how can I override this operator '[ ][ ]' s.t. T[i][j] can be used?
You make operator[] return a row-proxy type, which in turn has
an operator[] that returns a double& for the entry. See the
FAQ for details.
Note: this is not entirely trivial (especially if you care
about const-correctness).
Given that double* and double const* both have an overloaded [],
and can work as a proxy, it's hard to say that it's not trivial.
Expanding to three or more dimensions isn't entirely trivial, of
course, and nor is adding bounds checking, but neither are that
difficult either.
It didn't occur to me to use double * or double const * as
row-proxys, and upon reflection, I don't think it's a good
idea. Expressions such as *M[4] and M[5]+1 (where M is a
myMatrix) should be rejected by the compiler.
It depends on your goals. If the main goal is to get something
up and running quickly, in order to experiment with, then
double* is fine. If robustness is part of the goal, then no; at
the very least, you'd want to have assertion checks on the
bounds.
Of course, none of this is rocket science. However, the main
question remains: why not just use a matrix library?
Quite. Code that someone else has already written and debugged
always takes less time to develop. And in this case, you
shouldn't have too much problem finding code that is already
widely used, so (hopefully at least) any serious errors will
already have been discovered and corrected.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34