Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!ahodgson From: ahodgson@athena.mit.edu (Antony Hodgson) Newsgroups: comp.lang.c++ Subject: Re: Matrix Class Libraries Message-ID: <1991Mar21.151635.21044@athena.mit.edu> Date: 21 Mar 91 15:16:35 GMT References: <1991Mar18.025539.18998@athena.mit.edu> <6732@celery34.UUCP> Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 28 In article <6732@celery34.UUCP> stephens@motcid.UUCP (Kurt Stephens) writes: >Ari.Huttunen@hut.fi (Ari Juhani Huttunen) writes: stuff about how he wants to use [][] to get access to matrix elements rather than (int,int). > >Just define: > >inline >double* >Matrix::operator [](int i) { return Row(i); } > >Matrix M; >double d; > > d = M[1][2]; > >"M[1]" evals to a double*, right? The "[2]" indexes off the double*. >KISS (Keep It Simple Stephens) ;^) > In this case, the solution is too simple. The problem with returning a double* is that you can no longer perform bounds checking on the second index (the index i in operator[] should really be checked for validity as well). Of course, if you don't want to do bounds checking, this solution is fine. Tony Hodgson ahodgson@hstbme.mit.edu