自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++模板常用使用方法介紹

開發(fā) 后端
我們?cè)谶@篇文章中為大家總結(jié)了一些C++模板的常用方法,比如C++模板類靜態(tài)成員;C++模板類偏特化;類模版+函數(shù)模版等。

C++編程語言中的模板應(yīng)用在一定程度上大大提高了程序開發(fā)的效率。我們?cè)谶@篇文章中為大家詳細(xì)講解一下有關(guān)C++模板的基本概念,希望初學(xué)者們可以通過本文介紹的內(nèi)容充分掌握這方面的知識(shí)。

前段時(shí)間重新學(xué)習(xí)C++,主要看C++編程思想和C++設(shè)計(jì)新思維。對(duì)模版的使用有了更進(jìn)一層的了解,特總結(jié)如下:

下面列出了C++模板的常用情況:

1. C++模板類靜態(tài)成員

  1. template < typename T> struct testClass   
  2. {   
  3. static int _data;   
  4. };   
  5. template< > int testClass< char>::_data = 1;   
  6. template< > int testClass< long>::_data = 2;   
  7. int main( void ) {   
  8. cout < <  boolalpha < <  (1==testClass< char>::_data) < <  endl;   
  9. cout < <  boolalpha < <  (2==testClass< long>::_data) < <  endl;   
  10. }  

2. C++模板類偏特化

  1. template < class I, class O> struct testClass   
  2. {   
  3. testClass() { cout < <  "I, O" < <  endl; }   
  4. };   
  5. template < class T> struct testClass< T*, T*>   
  6. {   
  7. testClass() { cout < <  "T*, T*" < <  endl; }   
  8. };   
  9. template < class T> struct testClass< const T*, T*>   
  10. {   
  11. testClass() { cout < <  "const T*, T*" < <  endl; }   
  12. };   
  13. int main( void )   
  14. {   
  15. testClass< int, char> obj1;   
  16. testClass< int*, int*> obj2;   
  17. testClass< const int*, int*> obj3;   

3.類模版+函數(shù)模版

  1. template < class T> struct testClass   
  2. {   
  3. void swap( testClass< T>& ) { cout < <  "swap()" < <  endl; }   
  4. };   
  5. template < class T> inline void swap( testClass< T>& x, 
    testClass
    < T>& y )   
  6. {   
  7. x.swap( y );   
  8. }   
  9. int main( void )  
  10. {   
  11. testClass< int> obj1;   
  12. testClass< int> obj2;   
  13. swap( obj1, obj2 );   

4. 類成員函數(shù)模板

  1. struct testClass  
  2. {   
  3. template < class T> void mfun( const T& t )  
  4. {   
  5. cout < <  t < <  endl;   
  6. }   
  7. template < class T> operator T()   
  8. {   
  9. return T();   
  10. }   
  11. };   
  12. int main( void )   
  13. {   
  14. testClass obj;   
  15. obj.mfun( 1 );   
  16. int i = obj;   
  17. cout < <  i < <  endl;   

5. 缺省C++模板參數(shù)推導(dǎo)

  1. template < class T> struct test   
  2. {   
  3. T a;   
  4. };   
  5. template < class I, class O=test< I> > struct testClass   
  6. {   
  7. I b;   
  8. O c;   
  9. };   
  10. void main()  
  11. {  

6. 非類型C++模板參數(shù)

  1. template < class T, int n> struct testClass {   
  2. T _t;   
  3. testClass() : _t(n) {   
  4. }   
  5. };   
  6. int main( void ) {   
  7. testClass< int,1> obj1;   
  8. testClass< int,2> obj2;   

7. 空模板參數(shù)

  1. template < class T> struct testClass;   
  2. template < class T> bool operator==( const testClass< T>&, 
    const testClass
    < T>& )   
  3. {   
  4. return false;   
  5. };   
  6. template < class T> struct testClass   
  7. {   
  8. friend bool operator== < >
    ( const testClass&, const testClass& );   
  9. };   
  10. void main()  
  11. {  

8. template template 類

  1. struct Widget1   
  2. {   
  3. template< typename T>   
  4. T foo(){}   
  5. };   
  6. template< template< class T>class X>   
  7. struct Widget2  
  8. {   
  9. };   
  10. void main()  
  11. {  
  12. cout< <  3 < <  '\n';  

以上就是對(duì)C++模板的一些常用方法的介紹。

【編輯推薦】

  1. C++存儲(chǔ)區(qū)域基礎(chǔ)概念詳解
  2. C++ typedef使用方法總結(jié)
  3. C++調(diào)用C鏈接庫具體應(yīng)用技巧講解
  4. C++類成員相關(guān)應(yīng)用方法介紹
  5. C++循環(huán)語句基本概念詳解
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-01-25 14:10:21

C++堆棧

2011-07-13 11:34:58

CC++時(shí)間函數(shù)

2010-01-26 17:35:09

C++棧

2010-02-02 09:32:32

C++ typedef

2011-07-20 13:34:37

C++

2009-12-30 17:47:54

Ubuntu常用命令

2009-08-28 17:01:43

C#構(gòu)造函數(shù)

2010-03-02 16:58:11

AJAX WCF服務(wù)項(xiàng)

2011-05-17 16:20:46

C++

2011-03-30 10:41:11

C++數(shù)據(jù)庫

2010-03-10 19:18:10

Python scri

2023-09-12 07:38:36

C++getline函數(shù)

2010-02-03 16:35:45

C++回文

2009-12-29 10:40:36

ADO組件

2009-12-25 17:10:51

WPF動(dòng)態(tài)資源

2013-03-25 11:00:35

互聯(lián)網(wǎng)開發(fā)

2024-01-23 10:48:44

C++函數(shù)重載開發(fā)

2010-02-04 10:52:36

C++字符串分割函數(shù)

2024-07-10 18:51:52

2010-01-27 14:18:41

Android智能指針
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)