掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流
C++編程語(yǔ)言中的模板應(yīng)用在一定程度上大大提高了程序開(kāi)發(fā)的效率。我們?cè)谶@篇文章中為大家詳細(xì)講解一下有關(guān)C++模板的基本概念,希望初學(xué)者們可以通過(guò)本文介紹的內(nèi)容充分掌握這方面的知識(shí)。

10年專(zhuān)注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站建設(shè),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專(zhuān)注于成都企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁(yè)制作,對(duì)成都小攪拌車(chē)等多個(gè)方面,擁有豐富的網(wǎng)站維護(hù)經(jīng)驗(yàn)。
前段時(shí)間重新學(xué)習(xí)C++,主要看C++編程思想和C++設(shè)計(jì)新思維。對(duì)模版的使用有了更進(jìn)一層的了解,特總結(jié)如下:
下面列出了C++模板的常用情況:
1. C++模板類(lèi)靜態(tài)成員
- 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++模板類(lèi)偏特化
- 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.類(lèi)模版+函數(shù)模版
- template < class T> struct testClass
- {
- void swap( testClass< T>& ) { cout < < "swap()" < < endl; }
- };
- template < class T> inline void swap( testClass< T>& x,
testClass< T>& y )- {
- x.swap( y );
- }
- int main( void )
- {
- testClass< int> obj1;
- testClass< int> obj2;
- swap( obj1, obj2 );
- }
4. 類(lèi)成員函數(shù)模板
- 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++模板參數(shù)推導(dǎo)
- template < class T> struct test
- {
- T a;
- };
- template < class I, class O=test< I> > struct testClass
- {
- I b;
- O c;
- };
- void main()
- {
- }
6. 非類(lèi)型C++模板參數(shù)
- template < class T, int n> struct testClass {
- T _t;
- testClass() : _t(n) {
- }
- };
- int main( void ) {
- testClass< int,1> obj1;
- testClass< int,2> obj2;
- }
7. 空模板參數(shù)
- template < class T> struct testClass;
- template < class T> bool operator==( const testClass< T>&,
const testClass< T>& )- {
- return false;
- };
- template < class T> struct testClass
- {
- friend bool operator== < >
( const testClass&, const testClass& );- };
- void main()
- {
- }
8. template template 類(lèi)
- struct Widget1
- {
- template< typename T>
- T foo(){}
- };
- template< template< class T>class X>
- struct Widget2
- {
- };
- void main()
- {
- cout< < 3 < < '\n';
- }
以上就是對(duì)C++模板的一些常用方法的介紹。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流