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)的指针
posted @ 2019-11-18 18:21  熊云港  阅读(151)  评论(0编辑  收藏  举报