函数模板

函数模板
用 函数模板 解决
template<class 类型参数1, class 类型参数2, … >
返回值类型  模板名 (形参表)
{
函数体

}

C++编译器遵循以下优先顺序:
Step 1: 先找参数完全匹配的普通函数(非由模板实例化
而得的函数)
Step 2: 再找参数完全匹配的模板函数
Step 3: 再找实参经过自动类型转换后能够匹配的普通
函数
Step 4: 上面的都找不到, 则报错

#include <iostream>
using namespace std;

template<class T1,class T2>
void print(T1 &a,T2 &b)
{
	cout<<"using the print1"<<endl;
}

template<class T>
void print(T &a,T&b)
{
	cout<<"using the print2"<<endl;
}

void print(int &a,int &b)
{
	cout<<"using the print"<<endl;
}
int main()
{
	int a=1,b=3;
	print(a,b);
	int c=1;double d=1.2;
	print(c,d);
	//system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值