1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > C++中函数模板template和函数参数为指针 且有返回值的结合使用

C++中函数模板template和函数参数为指针 且有返回值的结合使用

时间:2021-09-16 14:49:51

相关推荐

C++中函数模板template和函数参数为指针 且有返回值的结合使用

1 #include<iostream> 2 using namespace std; 3 // 利用模板函数计算一个表达式 4 template<class Type> 5 Type Abc(Type a,Type b,Type c) 6 { 7return a+b+c; 8 } 9 // 利用引用参数指针计算一个表达式10 template<class Type>11 Type ABC(Type *a,Type *b,Type *c)12 {13return (*a)+(*b)+(*c);14 15 }16 17 int main()18 {19int a=1,b=2,c=3;20cout<<"a= "<<a<<",b= "<<b<<",c= "<<c<<endl;21cout<<"使用函数模板计算表达式的结果为:\n";22cout<<Abc(a,b,c)<<endl;23float f=4,d=0.6,e=2.3;24cout<<"f= "<<f<<",d= "<<d<<",e= "<<e<<endl;25cout<<"使用函数模板计算表达式的结果为:\n";26cout<<Abc(f,d,e)<<endl;27int *p2,*q2,*r2;28p2=&a;29q2=&b;30r2=&c;3132cout<<"*p2= "<<*p2<<",*q2= "<<*q2<<",*r2= "<<*r2<<endl;33cout<<"使用引用参数指针计算表达式的结果为:"<<endl;34cout<<ABC(p2,q2,r2)<<endl;35return 0;36 }

程序中有两个模板函数,Type Abc(Type a,Type b,Type c),Type ABC(Type *a,Type *b,Type *c)两个的参数不同;

代码调试运行结果为:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。