函数指针

作者: lusoer | 来源:发表于2017-02-21 22:40 被阅读7次

    1.指针声明方式

    声明一个类型的函数指针:

    int (*pf)(int)//括号中是函数的形参类型
    int func(int a)
    {cout<<"I am a function"<<endl;
    return a;}
    pf=func;
    (*pf)(5);//利用函数地址使用函数
    pf(5);//the same as above
    

    声明一个函数指针很简单,只需要和平时声明函数的方式一样,把函数名换成(*pf)就表示一个函数指针。
    2.利用typedef关键字

    typedef const double *(*p_fun)(const double, int);
    const double *func(const double, int);
    p_fun p1 = func;//p_fun now is a type;
    

    相关文章

      网友评论

        本文标题:函数指针

        本文链接:https://www.haomeiwen.com/subject/yjcpwttx.html