0

I have a class that simulates 2D matrix through the use of nested vectors as follows:

In the class header file this is what I have:

template <typename T> class L1Matrix {
private:
  std::vector<std::vector<T> > mat;
  unsigned rows;
  unsigned cols;

public:
  L1Matrix(); /* emptry constructor */
  L1Matrix(unsigned _rows, unsigned _cols, const T& _initial);
  L1Matrix(const L1Matrix<T>& rhs);
  virtual ~L1Matrix();
  T& operator()(const unsigned& row, const unsigned& col);
  const T& operator()(const unsigned& row, const unsigned& col) const;
}

In the class declaration this is what I have:

// default constructor
template<typename T>
L1Matrix<T>::L1Matrix() : rows(0), cols(0) {};


template<typename T>
L1Matrix<T>::L1Matrix(unsigned _rows, unsigned _cols, const T& _initial) {
   mat.resize(_rows);
   for (unsigned i=0; i<mat.size(); i++) {
     mat[i].resize(_cols, _initial);
   }
   rows = _rows;
   cols = _cols;
}

template<typename T>
L1Matrix<T>::L1Matrix(const L1Matrix<T>& rhs) {
   mat = rhs.mat;
   rows = rhs.get_rows();
   cols = rhs.get_cols();
 }

template<typename T>
std::vector<std::vector<T> >::reference L1Matrix<T>::operator()(const unsigned& row, const unsigned& col) {
   return this->mat[row][col];
}

For sake of brevity I am not giving the rest of the class implementation here.

Now in the main code I am creating pointer to this 2D matrix as follows:

 L1Matrix<bool>* arr1;  // this is pointer to L1Matrix object
 L1Matrix<int> arr2(XSize, YSize, 0);  // L1Mtarix object

 arr1 = new L1Matrix<bool>(XSize, YSize, true);

Now for the actual L1Matrix object arr2 I can access the individual rows and columns like this: arr2(row,col) but my question is how to access the similar row and column elements using the L1Matrix pointer object arr1?

Please let me know. Thanks

srsci
  • 239
  • 5
  • 10

1 Answers1

0

You should be able to use (*arr1)(row, col)

mattnewport
  • 13,728
  • 2
  • 35
  • 39
  • Or the hard way `arr1->operator()(row,col)` – DaBrain Jun 05 '14 at 19:01
  • Also please see the implementation of the operator function () in the code. It returns a reference to the cell in question. But this complains if i try to get a reference to a bool data. – srsci Jun 05 '14 at 19:20