gridstring.h
来自「C++高级编程这本书所附的源代码」· C头文件 代码 · 共 117 行
H
117 行
#include "Grid.h" // The file containing the Grid template definition shown above#include <cstdlib>#include <cstring>using namespace std;template <int WIDTH, int HEIGHT>class Grid<char*, WIDTH, HEIGHT>{ public: Grid(); Grid(const Grid<char*, WIDTH, HEIGHT>& src); ~Grid(); Grid<char*, WIDTH, HEIGHT>& Grid<char*, WIDTH, HEIGHT>::operator=( const Grid<char*, WIDTH, HEIGHT>& rhs); void setElementAt(int x, int y, const char* inElem); char* getElementAt(int x, int y) const; int getHeight() const { return HEIGHT; } int getWidth() const { return WIDTH; } protected: void copyFrom(const Grid<char*, WIDTH, HEIGHT>& src); char* mCells[WIDTH][HEIGHT];};template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>::Grid(){ for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { // initialize each element to NULL mCells[i][j] = NULL; } }}template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>::Grid(const Grid<char*, WIDTH, HEIGHT>& src){ copyFrom(src);}template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>::~Grid(){ for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { delete [] mCells[i][j]; } }}template <int WIDTH, int HEIGHT>void Grid<char*, WIDTH, HEIGHT>::copyFrom( const Grid<char*, WIDTH, HEIGHT>& src){ int i, j; for (i = 0; i < WIDTH; i++) { for (j = 0; j < HEIGHT; j++) { if (src.mCells[i][j] == NULL) { mCells[i][j] = NULL; } else { mCells[i][j] = new char[strlen(src.mCells[i][j]) + 1]; strcpy(mCells[i][j], src.mCells[i][j]); } } }}template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>& Grid<char*, WIDTH, HEIGHT>::operator=( const Grid<char*, WIDTH, HEIGHT>& rhs){ int i, j; // check for self-assignment if (this == &rhs) { return (*this); } // free the old memory for (i = 0; i < WIDTH; i++) { for (j = 0; j < HEIGHT; j++) { delete [] mCells[i][j]; } } // copy the new memory copyFrom(rhs); return (*this);}template <int WIDTH, int HEIGHT>void Grid<char*, WIDTH, HEIGHT>::setElementAt( int x, int y, const char* inElem){ delete[] mCells[x][y]; if (inElem == NULL) { mCells[x][y] = NULL; } else { mCells[x][y] = new char[strlen(inElem) + 1]; strcpy(mCells[x][y], inElem); }}template <int WIDTH, int HEIGHT>char* Grid<char*, WIDTH, HEIGHT>::getElementAt(int x, int y) const{ if (mCells[x][y] == NULL) { return (NULL); } char* ret = new char[strlen(mCells[x][y]) + 1]; strcpy(ret, mCells[x][y]); return (ret);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?