// -*- coding: utf-8 -*- // Time-stamp: // Abstract class for representation of 2d arrays // Two derived classes provide implementation. #ifndef REP_HPP #define REP_HPP //#include "iterator.hpp" //#include "line.hpp" // class Iterator; template class Array2d; template class Iterator; template class Rep { // Classes Iterator and Array2d can access private fields friend Iterator; friend class Array2d; public: // Constructor from dimensions Rep(int m, int n) : nlin(m), ncol(n), refs(1) {} // Destructor virtual ~Rep() {} // Access operator // Each of this operator return an object of the internal class Line // This object of class line has also an operator[](int) which returns // a reference to the element of the given line. // Therefore, if a is an object of the class Rep // a[i][j] returns a reference of the value indexed by the pair (i,j) // The evaluation of a[i] return an object line of class Line which // contains a pointer to the beginning of the line and its length. // Then the expression line[j] returns the reference to the element. // Accessor for non-const objects class Line { public: // Constructor Line(T* q, int n) : p(q), ncol(n) {} // Access operators T& operator[](int j); const T& operator[](int j) const; private: T* p; // Pointer to the beginning of the line int ncol; // Number of columns }; virtual Line operator[](int i) = 0; // Accessor for const objects virtual const Line operator[](int i) const = 0; protected: // Clone the representation virtual Rep* clone() const = 0; const int nlin; // Number of lines const int ncol; // Number of columns private: int refs; // Reference count }; // Access operator template T& Rep::Line::operator[](int j) { if (0 <= j && j < ncol) return p[j]; else throw new exception(); } // Access operator to const array template const T& Rep::Line::operator[](int j) const { if (0 <= j && j < ncol) return p[j]; else throw new exception(); } #endif