⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridptr.h

📁 C++高级编程这本书所附的源代码
💻 H
字号:
#include "Grid.h"template <typename T>class Grid<T*>{    public:        Grid(int inWidth = kDefaultWidth, int inHeight = kDefaultHeight);        Grid(const Grid<T*>& src);        ~Grid();        Grid<T*>& operator=(const Grid<T*>& rhs);        void setElementAt(int x, int y, const T* inElem);        T* getElementAt(int x, int y) const;        int getHeight() const { return mHeight; }        int getWidth() const { return mWidth; }        static const int kDefaultWidth = 10;        static const int kDefaultHeight = 10;    protected:        void copyFrom(const Grid<T*>& src);        T** mCells;        int mWidth, mHeight;}; template <typename T>const int Grid<T*>::kDefaultWidth;template <typename T>const int Grid<T*>::kDefaultHeight;template <typename T>Grid<T*>::Grid(int inWidth, int inHeight) : mWidth(inWidth), mHeight(inHeight){  mCells = new T* [mWidth];  for (int i = 0; i < mWidth; i++) {    mCells[i] = new T[mHeight];  }}template <typename T>Grid<T*>::Grid(const Grid<T*>& src){    copyFrom(src);}template <typename T>Grid<T*>::~Grid(){    // free the old memory    for (int i = 0; i < mWidth; i++) {        delete [] mCells[i];    }    delete [] mCells;}template <typename T>void Grid<T*>::copyFrom(const Grid<T*>& src){    int i, j;    mWidth = src.mWidth;    mHeight = src.mHeight;    mCells = new T* [mWidth];    for (i = 0; i < mWidth; i++) {        mCells[i] = new T[mHeight];    }    for (i = 0; i < mWidth; i++) {        for (j = 0; j < mHeight; j++) {            mCells[i][j] = src.mCells[i][j];        }    }}template <typename T>Grid<T*>& Grid<T*>::operator=(const Grid<T*>& rhs){    // check for self-assignment    if (this == &rhs) {        return (*this);    }    // free the old memory    for (int i = 0; i < mWidth; i++) {        delete [] mCells[i];    }    delete [] mCells;    // copy the new memory    copyFrom(rhs);    return (*this);}template <typename T>void Grid<T*>::setElementAt(int x, int y, const T* inElem){    mCells[x][y] = *inElem;}template <typename T>T* Grid<T*>::getElementAt(int x, int y) const{    T* newElem = new T(mCells[x][y]);    return (newElem);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -