//?mytemplate.h?文件
#ifndef?_MYTEMPLATE_H_
#define?_MYTEMPLATE_H_
template<typename?_T>
class?mytemplate{
????T?val;
public:
????void?display();
}
#include?"mytemplate.cpp"
#endif
//?文件?mytemplate.cpp
#ifdef?_MYTEMPLATE_H_
template<typename?T>
void?mytemplate<T>::display(){
?????cout<<val<<endl;
}
#endif
這樣做的好處是我只需改變一個(gè)地方就可以使模板有不同的實(shí)現(xiàn),避免了
把不同的實(shí)現(xiàn)放在同一個(gè)文件里時(shí)難以避免的預(yù)編譯標(biāo)記(那些預(yù)編譯標(biāo)記
常常很難用,因?yàn)橛涀∷鼈兌己苈闊?,而且文件短小,閱讀維護(hù)起來更
方便(一個(gè)例子是你需要比較模板的不同實(shí)現(xiàn)的綜合性能時(shí),把它的幾個(gè)實(shí)現(xiàn)
都寫到一個(gè)文件里常常會把人搞糊涂).
不管怎樣做,程序必須在聲明模板實(shí)例以前就"看見"了模板的實(shí)現(xiàn)部分
posted on 2006-07-09 16:38
豪 閱讀(726)
評論(0) 編輯 收藏 引用 所屬分類:
Basic C++