Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!ut-emx!nowhere!sking From: sking@nowhere.uucp (Steven King) Newsgroups: comp.lang.c++ Subject: Re: Matrix Class Libraries Message-ID: <1991Mar19.161203.14680@nowhere.uucp> Date: 19 Mar 91 16:12:03 GMT References: <1991Mar18.025539.18998@athena.mit.edu> <1991Mar18.213545.20966@athena.mit.edu> Organization: American Anarchist Union Lines: 60 In article <1991Mar18.213545.20966@athena.mit.edu> bjaspan@athena.mit.edu (Barr3y Jaspan) writes: >In article , Ari.Huttunen@hut.fi (Ari Juhani Huttunen) writes: >|> I think this points out one problem in C++. You really should be able to >|> say: >|> inline double& operator[][](int, int) const; >|> >|> The problem isn't very serious, but it just isn't natural to access array >|> members using notation array(x,y), but array[x][y]. > >At the risk of starting a religous war, I'll respond. :-) > >In this particular case, the first method is preferable. Being able to say >array[x][y] implies (in the conventional C style) that array[x] is meaningful >(it always is in C). For my Matrix class, it isn't meaningful (you could >argue that I should have derived Matrix from Vector, in which case array[x] >could be meaningful, but that isn't the point.) > >So if you really always wanted to use [] for array indexing and () only for >function calls, [] would have to accept any number and type of arguments (ie: >operator[](int, int).) But then it would be semantically identical to () >anyway, so what's the point? > Okay, at the risk of fanning the flames of discussion... why not: class Matrix { double *d ; int M_m, M_n ; public: class aux { double *vec ; aux ( double *v ) { vec = v ; } friend class Matrix ; public: double& operator [] ( unsigned n ) { return vec [ n ] ; } } ; aux operator [] ( unsigned n ) { return aux ( &d [ n * M_m ] ) ; } } ; Obviously, various bounds checking etc., should be included. This then does allow for statements of the form: Matrix m ; m [j][i] = 1.0 ; Of course it does create an extra temporary and and a copy assignment of the matrix aux, but it does give that syntactical sugaring... >In any case, I agree that it isn't a serious problem. > >-- >Barr3y Jaspan, bjaspan@mit.edu >Watchmaker Computing -- Look Ma! No .sig! ..!cs.utexas.edu!ut-emx!nowhere!sking