博客
关于我
函数指针
阅读量:315 次
发布时间:2019-03-04

本文共 1877 字,大约阅读时间需要 6 分钟。

函数指针详解

函数指针在程序开发中扮演着重要角色,尤其是在需要动态调用函数或传递函数作为参数时。本文将从基础到应用,全面解析函数指针的使用方法。

1. 函数指针的基础

函数指针与其他指针一样,用于存储函数在内存中的地址。与对象指针不同,函数指针指向的是函数的代码段,而非数据段。函数指针的类型由函数的返回类型和形参类型决定,与函数名无关。

2. 函数指针的声明

函数指针的声明通常使用*符号表示指针。例如,以下声明表示lengthCompare函数返回bool类型,接受两个const string&型参数:

bool lengthCompare(const string &, const string &);

函数指针的声明方式有多种:

  • 使用通用指针类型
    bool (*pf)(const string &, const string &);
  • 使用类型别名
    using F = int(int *, int);
  • 使用typedef定义函数指针
    typedef decltype(lengthCompare) *FuncP;
  • 使用别名来定义函数指针
    typedef bool Func(const string &, const string &);
  • 3. 函数指针的初始化

    函数指针的初始化需要明确指向具体的函数。以下是常见的方法:

  • 直接赋值
    pf = lengthCompare;
  • 使用引用传递
    pf = &lengthCompare;
  • 初始化为零指针
    pf = 0;
  • 4. 函数指针的调用

    函数指针的调用需要严格遵守参数和返回类型的匹配规则。以下是几种常见的调用方式:

  • 直接调用
    bool b1 = pf("hello", "goodbye");
  • 使用间接调用( dereference operator )
    bool b2 = (*pf)("hello", "goodbye");
  • 直接调用原函数
    bool b3 = lengthCompare("hello", "goodbye");
  • 5. 函数指针的重载

    函数指针在函数重载时起着重要作用。编译器通过函数指针的类型来决定调用哪个函数。以下示例展示了如何使用函数指针进行重载:

    void ff(int *);void ff(unsigned int);void (*fn)(unsigned int) = ff;

    6. 函数指针的形参

    函数指针的形参不支持直接定义函数类型,必须像数组一样进行转换。以下是典型的调用方式:

    void useBigger(const string &s1, const string &s2, bool pf(const string &, const string &));void useBigger(const string &s1, const string &s2, bool (*pf)(const string &, const string &));

    7. 类型别名的使用

    类型别名是定义函数指针类型的重要工具。以下是几种常见的类型别名定义方式:

  • 定义函数类型
    typedef bool Func(const string &, const string &);
  • 使用decltype定义函数类型
    typedef decltype(lengthCompare) Func2;
  • 定义函数指针类型
    typedef bool (*FuncP)(const string &, const string &);
  • 结合decltype和typedef定义函数指针
    typedef decltype(lengthCompare) *FuncP2;
  • 8. 返回函数指针

    有时我们需要将函数返回类型设置为函数指针。以下是一些常见的做法:

  • 使用using声明
    using F = int(int *, int);F *f1(int);
  • 直接定义返回类型
    PF f1(int);
  • 结合返回类型和函数名
    int (*f1(int))(int *, int);
  • 使用尾置返回类型
    auto f1(int) -> int(*)(int *, int);
  • 使用decltype简化返回类型
    decltype(lengthCompare) *f1(const string &s1, const string &s2);
  • 通过以上方法,我们可以灵活地处理函数指针的使用。函数指针为程序提供了更高的灵活性和可扩展性,是现代程序员的重要工具。

    转载地址:http://hkpq.baihongyu.com/

    你可能感兴趣的文章
    phoenix_执行sql报错_Error: ERROR 504 (42703): Undefined column. columnName=(state=4270_大数据工作笔记0181
    查看>>
    phoenix启动失败_The history file `/root/.sqlline/history` may be an older history---记录024_大数据工作笔记0184
    查看>>
    Phoenix基础命令_视图映射和表映射_数字存储问题---大数据之Hbase工作笔记0036
    查看>>
    phoenix无法连接hbase shell创建表失败_报错_PleaseHoldException: Master is initializing---记录020_大数据工作笔记0180
    查看>>
    Phoenix简介_安装部署_以及连接使用---大数据之Hbase工作笔记0035
    查看>>
    phoenix连接hbase报错Can not resolve hadoop120, please check your network_记录026---大数据工作笔记0187
    查看>>
    Photoshop工作笔记001---Photoshop常用快捷键总结
    查看>>
    Reids配置文件redis.conf中文详解
    查看>>
    Photoshop脚本入门
    查看>>
    PHP
    查看>>
    Regular Expression Notes
    查看>>
    PHP $FILES error码对应错误信息
    查看>>
    PHP $_FILES函数详解
    查看>>
    PHP $_SERVER['HTTP_REFERER'] 获取前一页面的 URL 地址
    查看>>
    php & 和 & (主要是url 问题)
    查看>>
    php -- 魔术方法 之 判断属性是否存在或为空:__isset()
    查看>>
    php -- 魔术方法 之 获取属性:__get()
    查看>>
    php -树-二叉树的实现
    查看>>
    PHP -算法-二路归并
    查看>>
    php 2条不一样 的json数据 怎么放在一个json里面_如果你是PHP开发者,请务必了解一下Composer...
    查看>>