十五.模板(动态类型)(C++)

内容参考于《21天学通C++》(第八版)
不去纠结C++的原理和细节,从C的角度去学习C++,再通过C++项目去加深理解

十五.模板

1. 普通模板示例
#include<iostream>
#include<string>
using namespace std;
template < typename Type> const Type& GetMax(const Type& value1, const Type& value2)
{
	if (value1 > value2)
		return value1;
	else
		return value2;
}

template < typename Type> void DisplayComparison(const Type& value1, const Type& value2)
{
	cout << "GetMax(" << value1 << ", " << value2 << ") = ";
	cout << GetMax(value1, value2) << endl;
}

int main()
{
	int num1 = -101, num2 = 2011;
	DisplayComparison(num1, num2);

	double d1 = 3.14, d2 = 3.1416;
	DisplayComparison(d1, d2);

	string name1("Jack"), name2("John");
	DisplayComparison(name1, name2);

	return 0;
}

运行结果

GetMax(-101, 2011) = 2011
GetMax(3.14, 3.1416) = 3.1416
GetMax(Jack, John) = John

调用 DisplayComparison 时,也可显式地指定类型,如下所示:

DisplayComparison<int>(num1, num2);
2. 声明包含多个参数的模板
template <typename T1, typename T2> class HoldsPair
{
private:
	T1 value1;
	T2 value2;
public:
	// Constructor that initializes member variables
	HoldsPair(const T1& val1, const T2& val2)
	{
		value1 = val1;
		value2 = val2;
	};
	// ... Other member functions
};
3. 声明包含默认参数的模板
template <typename T1=int, typename T2=int> class HoldsPair
{
	// ... method declarations
};
4. 模板类
#include <iostream>
using namespace std;
// template with default params: int & double
template < typename T1 = int, typename T2 = double> class HoldsPair
{
private:
	T1 value1;
	T2 value2;
public:
	HoldsPair(const T1& val1, const T2& val2) // constructor
		: value1(val1), value2(val2) {}

	// Accessor functions
	const T1 & GetFirstValue() const
	{
		return value1;
	}

	const T2& GetSecondValue() const
	{
		return value2;
	}
};

int main()
{
	HoldsPair<> pairIntDbl(300, 10.09);
	HoldsPair<short, const char*>pairShortStr(25, "Learn templates, love C++");

		cout << "The first object contains -" << endl;
	cout << "Value 1: " << pairIntDbl.GetFirstValue() << endl;
	cout << "Value 2: " << pairIntDbl.GetSecondValue() << endl;

	cout << "The second object contains -" << endl;
	cout << "Value 1: " << pairShortStr.GetFirstValue() << endl;
	cout << "Value 2: " << pairShortStr.GetSecondValue() << endl;

	return 0;
}

运行结果

The first object contains -
Value 1: 300
Value 2: 10.09
The second object contains -
Value 1: 25
Value 2: Learn templates, love C++
5. 模板类实例化和具体化

模板类是创建类的蓝图,因此在编译器看来,仅当模板类以某种方式被使用后,其代码才存在。换言之,对于您定义了但未使用的模板类,编译器将忽略它。

#include <iostream>
using namespace std;
// 模板
template < typename T1 = int, typename T2 = double> class HoldsPair
{
private:
	T1 value1;
	T2 value2;
public:
	HoldsPair(const T1& val1, const T2& val2) // constructor
		: value1(val1), value2(val2) {}

	// Accessor functions
	const T1 & GetFirstValue() const;
	const T2& GetSecondValue() const;
};

// 具体化
// specialization of HoldsPair for types int & int here
template<> class HoldsPair < int, int>
{
private:
	int value1;
	int value2;
	string strFun;
public:
	HoldsPair(const int& val1, const int& val2) // constructor
		: value1(val1), value2(val2) {}

	const int & GetFirstValue() const
	{
		cout << "Returning integer " << value1 << endl;
		return value1;
	}
};

int main()
{
	HoldsPair<int, int> pairIntInt(222, 333);
	pairIntInt.GetFirstValue();

	return 0;
}

运行结果

Returning integer 222
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值