typename場合、默認模板參數、趣味寫法分析

005typename的應用場合_用在模板函數類型成員前表示是一個類型

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

//求字符串的長度
template<typename T>
typename T::size_type GetLength(const T&c)
{
	if (c.empty())
	{
		return 0;
	}
	return c.size();
}


int main(void)
{
	string mytest = "jisuanjizuchegnyuanli";
	string::size_type size = GetLength(mytest);//等價於無符號整形

	
	system("pause");
	return 0;
}

/*
*(1)typename的使用場合
*	1.在模板定義裏面。typename標明其後的參數是類型參數		template<typename T,int a,int b>
*	template<typename T>		//名字爲T的模板參數
*	typename 可以使用class,這裏的class不是類定義,表示類型參數
*	2.使用類的類型成員,用typename來表示這是一個類型
*	::可以表示類成員作用域
*	::還可以表示訪問模板類的類型成員。
*				函數返回值
*		typename myVector<T>::myIterator   myVector<T>::myend()
*		typename 的用處就是顯式告訴編譯器myVector<T>::myIterator是一個類型
*
		typename T::size_type GetLength(const T&c)
		中的T::size_type也是類型,所以前面需要加上typename
*
*(2)
*
*(3)
*
*
*/

006函數指針調用函數

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>


using namespace std;
int myFunction(int a,int b)
{
	return  a + b;
}
//把函數指針作爲另外一個函數的參數傳遞??
//
//定義一個函數指針類型
typedef int(*FunType)(int, int);
//定義一個函數接收函數的指針
void TestFunction(int i,int j,FunType myfun)
{
	//這裏可以通過函數指針調用函數
	int result = myfun(i, j);
	cout << result << endl;
}


int main(void)
{
	TestFunction(1, 2, myFunction);//函數名字就相當於一個函數地址
	TestFunction(1, 2, &myFunction);//ok。取地址也可以調用成功

	system("pause");
	return 0;
}

/*
*(1)函數指針做其他函數的參數
*
*
*(2)
*
*(3)
*
*
*/

007函數模板趣味用法_傳遞一個可調用類對象作爲類模板參數

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

int myFunction(int a, int b)
{
	return  a + b;
}


//把函數指針作爲另外一個函數的參數傳遞??
//
//定義一個函數指針類型
typedef int(*FunType)(int, int);

template <typename T,typename F>
void testFunction(const T&i,const T&j,F myfunc)
{
	cout << myfunc(i, j) << endl;
}

class tc
{
public:
	tc()
	{
		cout << "無參構造函數執行" << endl;
	}
	tc(const tc&t)
	{
		cout << "拷貝構造函數執行" << endl;
	}
	//重載()函數調用運算符
	int operator()(int a,int b)
	{
		return a + b;
	}
public:
	
};



int main(void)
{
	testFunction(1, 2,myFunction);//自動推斷 T爲int,F爲函數類型


	tc object01;
	testFunction(3, 4, object01);
	/*
	 * 1.首先調用拷貝構造函數執行,把一個object01拷貝給形參,判斷爲類 類型
	 * 2.然後調用重載的函數調用()運算符
	 */
	cout << "_______________________________" << endl;
	testFunction(5, 6, tc());//使用一個臨時對象,少調用一個拷貝構造函數,只調用了無參構造函數

	
	system("pause");
	return 0;
}

/*
 *傳遞一個可調用類對象作爲類模板參數
 *
*(1)可調用對象概念--具體見“未歸類知識點1”
*	1.如果類重載了函數調用運算符(也就是括號運算符),則我們可以像使用函數一樣使用該類的對象,
*		我們把這樣的類對象稱爲函數對象(function object)
*	2.C++語言中有幾種可調用對象:函數、函數指針、lamba表達式、
*		bind創建的對象以及重載了函數運算符的類。
*
*(2)
*
*(3)
*
*
*/

008默認模板參數

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include"myArray.h"
using namespace std;

int myFunction(int a, int b)
{
	return  a + b;
}


//把函數指針作爲另外一個函數的參數傳遞??
//
//定義一個函數指針類型
typedef int(*FunType)(int, int);

template <typename T, typename F=FunType>//聲明的時候指定類型
void testFunction(const T&i, const T&j, F myfunc=myFunction)//有默認參數
{
	cout << myfunc(i, j) << endl;
}

int main(void)
{
	//a
	myArray<> my_array01;//完全用模板參數的默認缺省值
	myArray<int >my_array02;//第二個參數使用默認缺省值


	//b.函數模板的默認參數 testFunction最後參數有默認值
	testFunction(3, 4);
	
	system("pause");
	return 0;
}

/*
*(1)默認模板參數
*	a.類模板--類模板名字後面必須使用<>來提供額外的信息。<>表示這是一個模板
*	b.函數模板的默認參數
*		老標準只能爲類模板提供默認模板參數,c++11新標準可以爲函數模板提供默認參數
*(2)
*
*(3)
*
*
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章