c++ - 函数指针表达式

标签 c++ pointers

所以我得到了以下表达式:

int (*f1(int(*a)(int, int))) (int, int);

我正试图弄清它的意义,但它令人困惑。我发现“a”是一个指向带有 2 个参数(int,int)的函数的指针。 然后 f1 似乎是指向另一个函数的指针,该函数接受 2 个 int 参数。 但令我困惑的是 f1 与 a 的关系。

谁能给我一些提示或正确解释上面的表达式在做什么?

最佳答案

它将 f1 声明为一个带有名为 a 的参数的函数。参数类型和返回类型都是“指向函数的指针,带有两个返回 intint 参数”。


这是你解析它的方式:

// f1 is...
      f1
// ...a function...
      f1(                 )
// ...with a single parameter called `a`, which is...
      f1(     a           )
// ...a pointer to...
      f1(    *a           )
// (skip parentheses)
      f1(   (*a)          )
// ...a function...
      f1(   (*a)(        ))
// ...with two `int` parameters...
      f1(   (*a)(int, int))
// ...returning an `int`. The `f1` itself returns...
      f1(int(*a)(int, int))
// ...a pointer to...
     *f1(int(*a)(int, int))
// (skip parentheses)
    (*f1(int(*a)(int, int)))
// ...a function...
    (*f1(int(*a)(int, int))) (        )
// ...with two int parameters...
    (*f1(int(*a)(int, int))) (int, int)
// ...returning an `int`.
int (*f1(int(*a)(int, int))) (int, int)

关于c++ - 函数指针表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240050/

相关文章:

c++ - 如何拥有按值 vector 并结合使用指针 vector ?

c++ - 指针 (*and) 语法引用

c++ - 使用 cin.get 获取整数

c++ - 将 vector 传递给模板函数

c++ - 如何调用保存在字符串中的函数

c++ - 为什么不使用 std::getline 就可以调用 getline?

c++ - Boost ASIO 服务器 - 检测空闲连接

c - 指针作为一个数组的索引 - C

c++ - 指针被意外修改

C函数签名问题