delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/19/10:47:08

From: Charles Krug <charles AT pentek DOT com>
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
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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<T> might be superior.


What I have so far:

template<class T>
matrix<T>
protected:
    vector<vector<T> > rows;

public:
    vector<T> 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<double> 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 <T> or a vector of pointers to
<T>?












--
Charles Krug, Jr.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019