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

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

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

代码调试运行结果为:

转载于:https://www.cnblogs.com/yuzhuwei/p/4172280.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值