测试代码:
#include<iostream>
using namespace std;
template<class X>void func(X a)
{
cout<<"In func_one/n";
}
template<class X>void func(X a,X b)
{
cout<<"In func_two/n";
}
int main()
{
int a=0,b=0;
func(a);
func(a,b);
return 0;
}
测试结果:
In func_one
In func_two
请按任意键继续. . .
本文展示了一个使用C++模板函数重载的例子。通过两个不同的模板函数定义,演示了如何根据传入参数的数量来选择合适的函数实现。测试代码运行结果显示,函数能够正确区分并调用对应的重载版本。

被折叠的 条评论
为什么被折叠?



