c++模板的应用

original url from below :

http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1

http://www.codeproject.com/Articles/268849/An-Idiots-Guide-to-Cplusplus-Templates-Part-2


编译环境: win7X64Sp1 + Vs2008 + unicode


/// 函数模板

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 函数模板
template<typename T>	///< typename~
T Twice(T In)
{
	return (In * 2);
}

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	int		iOut	=	0;
	float	fOut1	=	0.0f;
	float	fOut2	=	0.0f;

	iOut = Twice(static_cast<int>(66));
	fOut1 = Twice(static_cast<float>(66.66f));
	fOut2 = Twice(Twice(static_cast<float>(66.66f)));

	_tprintf(L"iOut = %d\n", iOut);
	_tprintf(L"fOut1 = %f\n", fOut1);
	_tprintf(L"fOut2 = %f\n", fOut2);

	/** Operating results
	iOut = 132
	fOut1 = 133.320007
	fOut2 = 266.640015
	*/
}


/// 多入参的函数模板

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 函数模板, 多入参
template<typename T>	///< typename~
T Add(T In1, T In2)
{
	return (In1 + In2);
}

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	int		iOut	=	0;
	float	fOut1	=	0.0f;

	iOut = Add(static_cast<int>(66), static_cast<int>(77));
	fOut1 = Add(66.66f, 77.77f);

	_tprintf(L"iOut = %d\n", iOut);
	_tprintf(L"fOut1 = %f\n", fOut1);

	/** Operating results
	iOut = 143
	fOut1 = 144.429993
	*/
}


/// 在函数模板中初始化模板类型变量

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 函数模板, 多入参, 在函数模板中使用模板数据类型
template<typename T>	///< typename~
T Add(T In1, T In2)
{
	T	tResult = T();	///< 模板类型变量初始化

	tResult = In1 + In2;

	return tResult;
}

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	int		iOut	=	0;
	float	fOut1	=	0.0f;

	iOut = Add(static_cast<int>(66), static_cast<int>(77));
	fOut1 = Add(66.66f, 77.77f);

	_tprintf(L"iOut = %d\n", iOut);
	_tprintf(L"fOut1 = %f\n", fOut1);

	/** Operating results
	iOut = 143
	fOut1 = 144.429993
	*/
}


/// 函数模板入参使用模板类型变量的数组

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 函数模板, 多入参, 在函数模板中使用模板数据类型的数组指针
template<typename T>	///< typename~
ULONGLONG Add(const T tArray[], size_t nSizeArray)
{
	T			tTmp	=	T();
	size_t		nIndex	=	0;
	ULONGLONG	ullOut	=	0;

	for (nIndex = 0; nIndex < nSizeArray; nIndex++)
	{
		 tTmp = tArray[nIndex];
		 ullOut += tTmp;
	}

	return ullOut;
}

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	ULONGLONG	ullSum	=	0;
	int			iArray[]	=	{1, 3, 5, 7, 9};
	DWORD		dwArray[]	=	{0xfffefffe, 0x1234, 0x7788, 0x9999, 0x77777777};

	ullSum = Add(iArray, sizeof(iArray) / sizeof(int));
	_tprintf(L"iArray's sum = 0x%I64X\n", ullSum);

	ullSum = Add(dwArray, sizeof(dwArray) / sizeof(DWORD));
	_tprintf(L"dwArray's sum = 0x%I64X\n", ullSum);


	/** Operating results
	iArray's sum = 0x19
	dwArray's sum = 0x177779ACA
	*/
}


/// 函数模板, 多类型的多入参

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 函数模板, 多类型的多入参
template<typename T1, typename T2, typename T3>	///< typename~
T3 Add(const T1 tArray[], T2 tSizeArray, T3 & tOut)	///< 模板出参类型需要在入参中给定
{
	T1			tTmp		=	T1();
	T2			tIndex		=	T2();

	for (tIndex = 0; tIndex < tSizeArray; tIndex++)
	{
		 tTmp = tArray[tIndex];
		 tOut += tTmp;
	}

	return tOut;
}

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	ULONGLONG	ullSum	=	0;
	int			iArray[]	=	{1, 3, 5, 7, 9};
	DWORD		dwArray[]	=	{0xfffefffe, 0x1234, 0x7788, 0x9999, 0x77777777};

	ullSum = Add(iArray, sizeof(iArray) / sizeof(int), ullSum);
	_tprintf(L"iArray's sum = 0x%I64X\n", ullSum);

	ullSum = Add(dwArray, sizeof(dwArray) / sizeof(DWORD), ullSum);
	_tprintf(L"dwArray's sum = 0x%I64X\n", ullSum);


	/** Operating results
	iArray's sum = 0x19
	dwArray's sum = 0x177779ACA
	*/
}


/// 显式指定模板函数的入参类型

void Test1()
{
	ULONGLONG	ullSum	=	0;
	int			iArray[]	=	{1, 3, 5, 7, 9};
	DWORD		dwArray[]	=	{0xfffefffe, 0x1234, 0x7788, 0x9999, 0x77777777};

	/// 显式指定模板函数的入参类型
	ullSum = Add<int,size_t,ULONGLONG>(iArray, sizeof(iArray) / sizeof(int), ullSum);
	_tprintf(L"iArray's sum = 0x%I64X\n", ullSum);

	ullSum = Add<DWORD,size_t,ULONGLONG>(dwArray, sizeof(dwArray) / sizeof(DWORD), ullSum);
	_tprintf(L"dwArray's sum = 0x%I64X\n", ullSum);


	/** Operating results
	iArray's sum = 0x19
	dwArray's sum = 0x177779ACA
	*/
}

/// 函数模板, 指定默认参数值

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 函数模板, 指定默认参数值
template<typename T1, typename T2, typename T3>	///< typename~
T3 Add(T3 & tOut, const T1 tArray[], T2 tSizeArray = T2(2))	///< 模板出参类型需要在入参中给定
{
	T1			tTmp		=	T1();
	T2			tIndex		=	T2();

	for (tIndex = 0; tIndex < tSizeArray; tIndex++)
	{
		 tTmp = tArray[tIndex];
		 tOut += tTmp;
	}

	return tOut;
}

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	ULONGLONG	ullSum	=	0;
	int			iArray[]	=	{1, 3, 5, 7, 9};
	DWORD		dwArray[]	=	{0x1234, 0x1111, 0x7788, 0x9999, 0x77777777, 0xfffefffe};

	/// 显示指定要累加整个数组所有元素
	ullSum = 0;
	ullSum = Add<int,size_t,ULONGLONG>(ullSum, iArray, sizeof(iArray)/sizeof(int));
	_tprintf(L"iArray's sum = 0x%I64X\n", ullSum);

	
	/// 默认是累加数组前2个元素
	ullSum = 0;
	ullSum = Add<DWORD,size_t,ULONGLONG>(ullSum, dwArray);
	_tprintf(L"dwArray's sum = 0x%I64X\n", ullSum);


	/** Operating results
	iArray's sum = 0x19
	dwArray's sum = 0x2345
	*/
}

类模板

#include <windows.h>
#include <tchar.h>
#include <iostream>

/// 类模板
template<class T1>
class CData
{
public:
	CData() : m_t1Data(T1(0))	///< 成员变量初始化不能使用成员函数
	{

	}

	~CData()
	{

	}

	void SetData(T1 t1In) {m_t1Data = t1In;}
	T1 GetData() {return m_t1Data;}

private:
	T1	m_t1Data;
};

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	CData<int>		CDataInt;
	CData<DWORD>	CDatadw;

	CDataInt.SetData(static_cast<int>(66));
	_tprintf(L"CDataInt.GetData() = %d\n", CDataInt.GetData());

	CDatadw.SetData(static_cast<DWORD>(0x66778899));
	_tprintf(L"CDatadw.GetData() = 0x%x\n", CDatadw.GetData());

	/** Operating results
	CDataInt.GetData() = 66
	CDatadw.GetData() = 0x66778899
	*/
}

/// 类模板的运算符重载

#include <windows.h>
#include <tchar.h>
#include <iostream>

#define DATA_ZERO		0
#define DATA_INVALID	-1

/// 类模板的运算符重载
template<class T1>
class CData
{
public:
	CData() : m_t1Data(T1(DATA_ZERO)), m_t1DataInvalid(T1(DATA_INVALID))	///< 成员变量初始化不能使用成员函数
	{

	}

	~CData()
	{

	}

	T1 operator[](size_t nIndex) const
	{
		return GetDataIndex(nIndex);
	}

	T1& operator[](size_t nIndex)
	{
		return GetDataIndex(nIndex);
	}

	void SetData(T1 t1In) {m_t1Data = t1In;}
	T1 GetData() {return m_t1Data;}

private:
	T1& GetDataIndex(size_t nIndex)
	{
		return (0 == nIndex) ? m_t1Data : m_t1DataInvalid;
	}

private:
	T1	m_t1Data;
	T1	m_t1DataInvalid;
};

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	CData<int>		CDataInt;
	CData<DWORD>	CDatadw;

	CDataInt.SetData(static_cast<int>(66));
	_tprintf(L"CDataInt.GetData() = %d\n", CDataInt.GetData());

	CDatadw.SetData(static_cast<DWORD>(0x66778899));
	_tprintf(L"CDatadw.GetData() = 0x%x\n", CDatadw.GetData());
	_tprintf(L"CDatadw.GetData() = 0x%x\n", CDatadw[0]);
	_tprintf(L"CDatadw.GetData() = 0x%x\n", CDatadw[99]);

	/** Operating results
	CDataInt.GetData() = 66
	CDatadw.GetData() = 0x66778899
	CDatadw.GetData() = 0x66778899
	CDatadw.GetData() = 0xffffffff
	*/
}

/// 在类模板中使用函数模板

#include <windows.h>
#include <tchar.h>
#include <iostream>

#define DATA_ZERO		0
#define DATA_INVALID	-1

/// 在类模板中使用函数模板
template<class T1>
class CData
{
public:
	CData() : m_t1Data(T1(DATA_ZERO))
	{

	}

	virtual ~CData()
	{

	}

	template<typename D1, typename D2>
	D2 add(D1 d1In, D2 & d2Sum)
	{
		d2Sum += d1In;
		return d2Sum;
	}


	void SetData(T1 t1In) {m_t1Data = t1In;}
	T1 GetData() {return m_t1Data;}

private:
	T1	m_t1Data;
};

void Test1();

int _tmain(int argc, _TCHAR* argv[])
{
	Test1();

	getchar();
	return 0;
}

void Test1()
{
	CData<int>		CDataInt;
	CData<DWORD>	CDatadw;
	ULONGLONG		ullSum		=	0;

	CDataInt.SetData(static_cast<int>(66));
	_tprintf(L"CDataInt.GetData() = %d\n", CDataInt.GetData());

	CDatadw.SetData(static_cast<DWORD>(0x66778899));
	_tprintf(L"CDatadw.GetData() = 0x%x\n", CDatadw.GetData());

	CDatadw.add<DWORD, ULONGLONG>(0x20, ullSum);
	CDatadw.add<DWORD, ULONGLONG>(0x30, ullSum);
	CDatadw.add<DWORD, ULONGLONG>(0x40, ullSum);
	_tprintf(L"ullSum = 0x%I64X\n", ullSum);

	/** Operating results
	CDataInt.GetData() = 66
	CDatadw.GetData() = 0x66778899
	ullSum = 0x90
	*/
}






你可能感兴趣的:(c++模板的应用)