为什么把几十个*前的函数指针或函数返回一个函数指针编译? [英] why put dozens of * before function pointer or function that return a function pointer compliles?

查看:123
本文介绍了为什么把几十个*前的函数指针或函数返回一个函数指针编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个代码处理多个函数指针解除引用意外编译,尽管似乎没有有效。这怎么可以编译?是编译器中的错误吗?我在Ubuntu 14.04上使用gcc 4.8.2。

I have found that this code dealing with multiple function pointer dereferences unexpectedly compiles, despite seeming not to be valid. How can this compile? Is it a bug in the compiler? I am using gcc 4.8.2 on Ubuntu 14.04.

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?

}


推荐答案

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

A function lvalue is implicitly convertible to a pointer to the function ([conv.func]). In your example, the function is converted to a pointer before each dereference.

这篇关于为什么把几十个*前的函数指针或函数返回一个函数指针编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆