onedgrid.h
来自「C++高级编程这本书所附的源代码」· C头文件 代码 · 共 97 行
H
97 行
template <typename T>class OneDGrid{ public: OneDGrid(int inSize = kDefaultSize); OneDGrid(const OneDGrid<T>& src); ~OneDGrid(); OneDGrid<T> &operator=(const OneDGrid<T>& rhs); void resize(int newSize); T& operator[](int x); const T& operator[](int x) const; int getSize() const { return mSize; } static const int kDefaultSize = 10; protected: void copyFrom(const OneDGrid<T>& src); T* mElems; int mSize;};template <typename T>const int OneDGrid<T>::kDefaultSize;template <typename T>OneDGrid<T>::OneDGrid(int inSize) : mSize(inSize){ mElems = new T[mSize];}template <typename T>OneDGrid<T>::OneDGrid(const OneDGrid<T>& src){ copyFrom(src);}template <typename T>OneDGrid<T>::~OneDGrid(){ delete [] mElems;}template <typename T>void OneDGrid<T>::copyFrom(const OneDGrid<T>& src){ mSize = src.mSize; mElems = new T[mSize]; for (int i = 0; i < mSize; i++) { mElems[i] = src.mElems[i]; }}template <typename T>OneDGrid<T>& OneDGrid<T>::operator=(const OneDGrid<T>& rhs){ // check for self-assignment if (this == &rhs) { return (*this); } // free the old memory delete [] mElems; // copy the new memory copyFrom(rhs); return (*this);}template <typename T>void OneDGrid<T>::resize(int newSize){ T* newElems = new T[newSize]; // Allocate the new array of the new size // Handle the new size being smaller or bigger than the old size for (int i = 0; i < newSize && i < mSize; i++) { // Copy the elements from the old array to the new one newElems[i] = mElems[i]; } mSize = newSize; // store the new size delete [] mElems; // free the memory for the old array mElems = newElems; // Store the pointer to the new array}template <typename T>T& OneDGrid<T>::operator[](int x){ return (mElems[x]);}template <typename T>const T& OneDGrid<T>::operator[](int x) const{ return (mElems[x]);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?