Template.doc_第1頁
Template.doc_第2頁
Template.doc_第3頁
Template.doc_第4頁
Template.doc_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

Templates 定義一個(gè)模板使用的關(guān)鍵字:class和typename。在定義作為模板的時(shí)候:class和typename,作用是一樣的。一 函數(shù)模板1 定義方式template function_declaration;template function_declaration;2 Exampletemplate T GetMax (T a, T b) T result;result = (ab)? a : b;return (result);3 使用方法function_name (parameters);int x,y;GetMax (x,y);對于內(nèi)部類型,通常是可以無需制定具體的類型,編譯器會(huì)自動(dòng)識(shí)別,而是寫成:int x,y;GetMax(x,y);/x,y為相同的類型int x;long y;GetMax(x,y);/x,y為不相同的類型 報(bào)錯(cuò) 兩種不同的內(nèi)部類型必須:template T GetMin (T a, U b) return (ab?a:b);int i,j;long l;i = GetMin (j,l);/或者i = GetMin (j,l);二 Class Template通常是使用模板作為類成員變量的類型1 Example:template class mypair T values 2; public: mypair (T first, T second)/內(nèi)聯(lián)函數(shù) values0=first; values1=second; ;mypair myobject (115, 36);/這樣使用mypair myfloats (3.0, 2.18);2 成員函數(shù)為非內(nèi)聯(lián)函數(shù)template class mypair T a, b; public: mypair (T first, T second) a=first; b=second; T getmax ();template /增加此聲明T mypair:getmax () T retval; retval = ab? a : b; return retval;int main () mypair myobject (100, 75); cout myobject.getmax(); return 0;模板類成員函數(shù)外部定義方法:template T mypair:getmax ()三 模板特化 Template Specialization1 將模板類轉(zhuǎn)化為特定類型相關(guān)的類/ class template:template class mycontainer T element; public: mycontainer (T arg) element=arg; T increase () return +element;/ class template specialization:template /class mycontainer /將其特例化為char型 char element; public: mycontainer (char arg) element=arg; char uppercase () if (element=a)&(element=z) element+=A-a; return element; ;int main () mycontainer myint (7); mycontainer mychar (j); cout myint.increase() endl; cout mychar.uppercase() endl; return 0;template class mycontainer . ;/模板類template class mycontainer . ;/特例化為char型template class mycontainer . ;/特例化為int型模板來與之相應(yīng)的特例化的類之間,并不是什么繼承關(guān)系,所以在特例化一個(gè)模板類時(shí),需要將其所有的包含成員進(jìn)行重寫,可以進(jìn)行擴(kuò)展。2 非模板類型參數(shù)Non-type parameters for templatestemplate /int Nclass mysequence T memblock N; public: void setmember (int x, T value); T getmember (int x);template void mysequence:setmember (int x, T value) memblockx=value;template T mysequence:getmember (int x) return memblockx;int main () mysequence myints; mysequence myfloats; myints.setmember (0,100); myfloats.setmember (3,3.1416); cout myints.getmember(0) n; cout myfloats.getmember(3) n; return 0;可以設(shè)置模板類型的缺省值:template class mysequence .;mysequence myseq;/可以缺省使用 mysequence myseq;/等同于上面四 Templates and multiple-file projectsFrom the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.模板不會(huì)在代碼編譯的時(shí)候直接進(jìn)行編譯,而是在被實(shí)例化一個(gè)特定類型的模板時(shí)候,才會(huì)編譯到,生成一個(gè)特定參數(shù)的函數(shù)。When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a header file with a .h extension, and the implementation (the definition of these functions) is in an independent file with c+ code. Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates. 由于模板特殊的編譯需要,模板類的定義和函數(shù)實(shí)現(xiàn)必須在同一個(gè)文件中進(jìn)行。Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.二 typename定義一個(gè)模板使用的關(guān)鍵字:class和typename。在定義作為模板的時(shí)候:class和typename,作用是一樣的。使用typename,實(shí)際上也是為模板服務(wù)的,在某些情況下我們需要明確的指出某些變量為模板。1 在一個(gè)模板類中使用另一個(gè)模板類定義一個(gè)變量時(shí)templatestruct first typedef T * pointer;templateclass second first:pointer p; / syntax error (VC+中 并不報(bào)錯(cuò));In a template, the name of a member of another class that depends on its template parameter(s) (first:pointerin this example, dependent on theTparameter) is adependent namethat i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論