zoukankan      html  css  js  c++  java
  • 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<<"使用函数模板计算表达式的结果为:
    ";
    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<<"使用函数模板计算表达式的结果为:
    ";
    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)两个的参数不同;

    代码调试运行结果为:

  • 相关阅读:
    STM32F2系列系统时钟默认配置
    在电源上叠加一个脉冲信号,模拟一个干扰信号
    const用法
    指向指针的指针
    Judge Route Circle
    汉明距离
    绘制三角形
    OpenGL工作流程
    OpenGL环境搭建
    next()方法 执行下一个中间件 类似than
  • 原文地址:https://www.cnblogs.com/yuzhuwei/p/4172280.html
Copyright © 2011-2022 走看看