#pragma once
#ifndef grid_h
#define grid_h
template
class grid
int getwidth() const
static const int kdefaultwidth = 10;
static const int kdefaultheight = 10;
protected:
void copyfrom(const grid& src);
t** mcells;
int mheight,mwidth;
};template
const int grid::kdefaultheight;
template
const int grid::kdefaultwidth;
template
grid::grid(int inwidth, int inheight):mwidth(inwidth),mheight(inheight)
}template
grid::grid(const grid& src)
template
grid::~grid()
delete mcells;
}template
void grid::copyfrom(const grid& src)
for(i = 0; i < mwidth; i++)}}
template
grid& grid::operator=(const grid& rhs)
//free the old memory
for(int i = 0; i < mwidth; i++)
delete mcells;
//copy the new memory
copyfrom(rhs);
return (*this);
}template
void grid::setelementat(int x, int y, const t& inelem)
template
t& grid::getelementat(int x, int y)
template
const t& grid::getelementat(int x, int y) const
#endif // grid_h
注意:c++ 模板類定義與宣告寫在同乙個檔案中,不然編譯通不過。
main函式中的呼叫:gridmyintgrid;
C 模板類定義與宣告
宣告和使用類模板 如果在類模板外定義成員函式,應寫成類模板形式 template 測試的模板類,如下 ifndef compare h define compare h template class compare endif include compare.h template compare c...
模板類 宣告與定義
模板類的宣告與定義 最近在編寫模板類時發現乙個問題 當把模板類分開為 h宣告檔案 和 cpp實現檔案時,在模板類的main檔案使用模板功能時,編譯結果完全正確,卻無法鏈結成功。但是,如果把main放到.cpp檔案中就沒有任何問題,或者把 cpp 和 h 檔案放在一起也沒有問題。於是摸索著學習,解決問...
模板類的宣告與定義
newhandlersupport.h 檔案 template class newhandlersupport template new handler newhandlersupport currenthandler template newhandlersupport newhandlersup...