From: Charles Krug Newsgroups: comp.os.msdos.djgpp,comp.lang.c++ Subject: 2d Matrix class with conventional c/c++ element access semantics Date: Mon, 19 Jan 1998 10:07:49 -0500 Lines: 68 Message-ID: <34C36C45.24966424@pentek.com> NNTP-Posting-Host: mail.pentek.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk I'd like to create a Matrix class with "drop-in" compatible accessor function semantics. I'm using djgpp w/ gcc2721b w/ lgp271b IOW-- The original code: // declare the matrix -- This also works if you use the "Numeric Recipies. . ." technique of dynamic matrix allocation double myMatrix[rows][columns]; // now I can iterate through the entire matrix this way for (int i = 0; i < rows; ++i) for (int j = 0; j < columns; ++j) // I can access myMatrix[i][j] here I'd like to create a matrix template class with the same access semantics. My thought was to use the NRIC technique, but it then occured to me that a template version using vector might be superior. What I have so far: template matrix protected: vector > rows; public: vector operator [] (unsigned int i) {return rows[i];} The idea being that the return value from the [] operator will be a vector that I can index using the [] operator, so I should be able to use matrix myMatrix(3,3) //BTW-the ctor seems to work okay // now I can iterate through the entire matrix this way for (int i = 0; i < rows; ++i) for (int j = 0; j < columns; ++j) // I can access myMatrix[i][j] here But I crash in my matrix access. Is there any way to do what I want? I want to copy the interface semantics of the conventional array while adding type safety. I could, of course, use the () operator for both indices, but that would defeat my objective, which is to graft this class into existing code with minimal complication. ALSO--should by row vector be a vector of or a vector of pointers to ? -- Charles Krug, Jr.