鮑松山STL源碼剖析 SGI版
2023-08-21 18:50 作者:bili_67158895267 | 我要投稿
模板
從模板生成的函數(shù)或類的定義是模板的實(shí)例或?qū)嵗?/p>
可以以內(nèi)聯(lián)的方式為類模板的成員函數(shù)指定一個外部模板
template<typename T>inline Array<T>::Array(cosnt Array& other)try:elements{new T[other.count]},count{other.count}{
? ?for (size_t i = 0; i < count; i++)
? ?{
? ? ? ?elements[i] = other.elements[i];
? ?}}catch(bad_alloc&){
? ?cerr<<"memory allocation failed for Array object copy."<<endl;}
每個類模板類型參數(shù)需要一個實(shí)參,除非有默認(rèn)實(shí)參
Array<int> data{40};
標(biāo)簽: