zoukankan      html  css  js  c++  java
  • 【C++】实例化Int(*(*F)(int,int))(int)_函数指针作返回类型

    Int(*(*F)(int,int))(int)的实现如下:

    ----------------------------------------------------

    typedef int (*pfun)(int);//定义类型——函数指针
    int fun1(int c=0)
    {
        return c+10;
    }
    int fun2(int c=0)
    {
        return c+20;
    }
    pfun choose(int a,int b)
    {
        pfun p;
        if(a>b)
            p=fun1;
        else
            p=fun2;
        return  p;
    }
    void main()
    {
        pfun (*F)(int,int)=choose;
        int a=1,b=2,c=3;
        cout<<F(a,b)(c)<<endl;//如果a>b 输出10+c,否则输出20+c
    }

    结论:

      ①函数的返回类型可以是函数指针,只不过需要借助typedef来定义这个类型;

      ②Int(*(*F)(int,int))(int)貌似只存在于逻辑里,不知代码中如何设计出现该语句,本文只出现了pfun (*F)(int,int);有待思考!

  • 相关阅读:
    cz_health_day07
    cz_health_day06
    mysql索引底层原理
    cz_health_day05
    redis无法获取连接原因分析
    cz_health_day04
    cz_health_day03
    cz_health_day02
    cz_health_day01
    Spring学习
  • 原文地址:https://www.cnblogs.com/caixu/p/2208748.html
Copyright © 2011-2022 走看看