zoukankan      html  css  js  c++  java
  • c++函数

    函数的定义由返回类型、函数名、形参表、函数体组成

    函数参数

    非引用类型的形参以响应实参的副本初始化,对(非引用)形参的任何修改仅作用于局部副本

    void main(void)
    {
        int a = 1;
        fun(a); //return 1
    }
    
    int fun(int a)
    {
        return ++a;
    }
    

    函数指针

    指针函数的初始化:只能通过同类型的函数/函数指针/0值来赋值(0代表不指向任何函数)

    int add(int nLeft,int nRight);//函数定义  
    int (*pf)(int,int);//未初始化 
    pf = add;//通过赋值使得函数指针指向某具体函数  
    

    使用typedef定义函数指针类型

    typedef int (*PF)(int,int);  
    PF pf;//此时,为指向某种类型函数的函数指针类型,而不是具体指针,用它可定义具体指针 
    

    通过指针调用函数

    PF mypf = add; // 调用未初始化或指向0的指针会出错
    add(1,2);
    mypf(1,2);
    (*mypf)(1,2);//三种相同
    

    函数指针形参

    //  等同
    void useBigger(const string&, const string&,bool (), bool(const string&, const string&));
    void useBigger(const string&, const string&,bool (), bool (*)(const string&, const string&));
    

    返回指向函数的指针

    //  由内往外:*ff(int)函数名为ff,参数为int,返回int (*)(int*, int)的函数指针
    int (*ff(int))(int *, int);
    //  使用别名
    typedef int (*PF)(int *, int);
    PF ff(int); //函数名ff,参数为int,返回int (*)(int*, int)的指针
    
  • 相关阅读:
    前端TypeScript编写的代码发布后怎么在浏览器中调试
    oracle中的执行计划
    oracle中的物化视图
    oracle中的exists 和not exists 用法
    Oracle中的索引详解
    Oracle中动态SQL拼接
    oracle 中sql优化的几种方法
    oracle中常用函数大全
    Oracle中游标的用法
    oracle中表分区的实现
  • 原文地址:https://www.cnblogs.com/xiongyungang/p/11883944.html
Copyright © 2011-2022 走看看