Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!apple!turk From: turk@Apple.COM (Ken "Turk" Turkowski) Newsgroups: comp.lang.c++ Subject: Re: operator[] for 2D array class Message-ID: <5890@internal.Apple.COM> Date: 20 Dec 89 05:30:08 GMT References: <21162@ut-emx.UUCP> Organization: Advanced Technology Graphics, Apple Computer, Cupertino, CA, USA Lines: 31 In article <21162@ut-emx.UUCP> ycy@walt.cc.utexas.edu (Joseph Yip) writes: >I am working on some routines that manipulate 2D array. In order to >access individual elements, I define a operator [] member function. >However, operator [] only takes one parameter. In order to do > A[i][j] = B[i][j] // A and B are two 2D array classes >I do: >Tmp Array2D::operator [] (int i) { > ... >} > >int& Tmp::operator[] (int j) { > ... >} > >In this way, the problem is solved. >Is there any better way to do this object array indexing? Yes; e.g. class 2DarrayOfElements { private: int nCols; element *M; public: element* operator [] (int i) { return(M + i * nCols); } }; -- Ken Turkowski @ Apple Computer, Inc., Cupertino, CA Internet: turk@apple.com Applelink: TURKOWSKI1 UUCP: sun!apple!turk