/////////////////////////////////////////////////////////////////// // circBuf.cpp - Solution of MT#4-Q2 // // // // This is a demo showing how to make the required declarations // // It provides no useful functionality. // /////////////////////////////////////////////////////////////////// template class circBuf { public: circBuf(int size); circBuf(const circBuf& cb); ~circBuf(); circBuf& operator=(const circBuf& cb); void add(const T& t); T& operator[](int n); T operator[](int n) const; private: int _size; // capacity of buffer int _last; // index of last element entered T* pBuf; }; //----< return n from the last element >----------------------------- template circBuf::circBuf(int size) : _size(size), pBuf(new T[size]) { } //----< return n from the last element >----------------------------- template circBuf::~circBuf() { delete [] pBuf; } //----< return n from the last element >----------------------------- template T& circBuf::operator[](int n) { if(n > size + _last) throw "domain error in index operator"; return (_last - n + size)%_size; } int main() { circBuf cb(10); return 0; }