// File: twodim14.h // // A two-dimensional array of int ADT. // This version IS templated AND has a nested SmartPtr class. // This time A[][] checks for out of bounds // The SmartIntPtr class is only used by TwoDim Array. // It does not allocate memory at all. // This class does not need a copy constructor or // overloaded assignment operator. #ifndef TWODIM_H #define TWODIM_H #include #include #include using namespace std ; // Used to throw an exception //class OutOfBounds : public exception { //class OutOfBounds : public logic_error { class OutOfBounds : public out_of_range { public: OutOfBounds() ; OutOfBounds(const string& str) ; ~OutOfBounds() throw() ; // guarantee that destructor won't throw // const char *what() throw() ; unsigned int bad_index ; unsigned int max_index ; private: // string m_what ; } ; // Forward templated class declaration template class TwoDimArray ; // Forward templated function declaration template ostream& operator << (ostream& out, TwoDimArray& A) ; template class TwoDimArray { friend ostream& operator << (ostream& out, TwoDimArray& A) ; public: // Constructors TwoDimArray() ; TwoDimArray(unsigned int r, unsigned int c) ; // Copy Constructor TwoDimArray(const TwoDimArray& A) ; // Destructor ~TwoDimArray() ; TwoDimArray& operator=(const TwoDimArray& rhs) ; void Print() ; T& at(unsigned int r, unsigned int c) ; private: class SmartPtr ; public: SmartPtr operator[] (unsigned int r) throw(OutOfBounds) ; private: unsigned int rows, cols ; T *M ; } ; template class TwoDimArray::SmartPtr { public: // Constructors SmartPtr() ; SmartPtr(T *ptr, unsigned int size) ; // Overload [] T& operator[] (unsigned int c) throw(OutOfBounds) ; private: T *p ; unsigned int n ; } ; #include "twodim14.cpp" #endif