为什么在函数指针或返回函数指针的函数编译前放上几十个*呢?

why put dozens of * before function pointer or function that return a function pointer compliles?

本文关键字:函数 指针 十个 返回 编译 为什么      更新时间:2023-10-16

我发现这段处理多个函数指针解引用的代码意外地编译了,尽管看起来是无效的。这是如何编译的?这是编译器中的错误吗?我在Ubuntu 14.04上使用gcc 4.8.2。

int addInt(int n,int m)      // function
{
    return n+m;
}
int (*(*functionFactoryPtr)(int n))(int, int); // pointer
int (*(functionFactory)(int n))(int, int)   // function
{
    std::cout << "Got parameter" <<  n << std::endl;
    int (*functionPtr)(int,int) = &addInt;
    return functionPtr;
}
int main()
{
    // functionFactoryPtr = @functionFactory;
    std::cout << (******(*****functionFactory)(4))(3,6) << std::endl;  // How is this not an error?
}

函数的左值隐式地转换为指向该函数的指针([conv.func])。在您的示例中,函数在每次解引用之前被转换为指针。