C++中函数模板template和函数参数为指针,且有返回值的结合使用
生活随笔
收集整理的這篇文章主要介紹了
C++中函数模板template和函数参数为指针,且有返回值的结合使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 #include<iostream>
2 using namespace std;
3 // 利用模板函數(shù)計算一個表達式
4 template<class Type>
5 Type Abc(Type a,Type b,Type c)
6 {
7 return a+b+c;
8 }
9 // 利用引用參數(shù)指針計算一個表達式
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<<"使用函數(shù)模板計算表達式的結果為:\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<<"使用函數(shù)模板計算表達式的結果為:\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<<"使用引用參數(shù)指針計算表達式的結果為:"<<endl;
34 cout<<ABC(p2,q2,r2)<<endl;
35 return 0;
36 }
程序中有兩個模板函數(shù),Type Abc(Type a,Type b,Type c),Type ABC(Type *a,Type *b,Type *c)兩個的參數(shù)不同;
代碼調(diào)試運行結果為:
轉載于:https://www.cnblogs.com/yuzhuwei/p/4172280.html
總結
以上是生活随笔為你收集整理的C++中函数模板template和函数参数为指针,且有返回值的结合使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串函数 replace() 方法妙
- 下一篇: ExtJs4学习(七)MVC中的Stor