网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++模板使用方法是什么

本篇文章为大家展示了C++模板使用方法是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联专业为企业提供石台网站建设、石台做网站、石台网站设计、石台网站制作等企业网站建设、网页设计与制作、石台企业网站模板建站服务,十余年石台做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率。我们为大家详细讲解一下有关C++模板的基本概念。

前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:

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

1. C++模板类静态成员

template < typename T> struct testClass   {   static int _data;   };   template< > int testClass< char>::_data = 1;   template< > int testClass< long>::_data = 2;   int main( void ) {   cout < <  boolalpha < <  (1==testClass< char>::_data) < <  endl;   cout < <  boolalpha < <  (2==testClass< long>::_data) < <  endl;   }

2. C++模板类偏特化

template < class I, class O> struct testClass   {   testClass() { cout < <  "I, O" < <  endl; }   };   template < class T> struct testClass< T*, T*>   {   testClass() { cout < <  "T*, T*" < <  endl; }   };   template < class T> struct testClass< const T*, T*>   {   testClass() { cout < <  "const T*, T*" < <  endl; }   };   int main( void )   {   testClass< int, char> obj1;   testClass< int*, int*> obj2;   testClass< const int*, int*> obj3;   }

3.类模版+函数模版

  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. 类成员函数模板

struct testClass  {   template < class T> void mfun( const T& t )  {   cout < <  t < <  endl;   }   template < class T> operator T()   {   return T();   }   };   int main( void )   {   testClass obj;   obj.mfun( 1 );   int i = obj;   cout < <  i < <  endl;   }

5. 缺省C++模板参数推导

template < class T> struct test   {   T a;   };   template < class I, class O=test< I> > struct testClass   {   I b;   O c;   };   void main()  {  }

6. 非类型C++模板参数

template < class T, int n> struct testClass {   T _t;   testClass() : _t(n) {   }   };   int main( void ) {   testClass< int,1> obj1;   testClass< int,2> obj2;   }

7. 空模板参数

  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 类

struct Widget1   {   template< typename T>   T foo(){}   };   template< template< class T>class X>   struct Widget2  {   };   void main()  {  cout< <  3 < <  '\n';  }

上述内容就是C++模板使用方法是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网页标题:C++模板使用方法是什么
转载来于:http://bjjierui.cn/article/pcchhj.html

其他资讯