如何使函数返回指向函数的指针?(C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/997821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:19:58  来源:igfitidea点击:

How to make a function return a pointer to a function? (C++)

c++return-valuefunction-pointers

提问by erikkallen

I'm trying to make a function that takes a character, then returns a pointer to a function depending on what the character was. I just am not sure how to make a function return a pointer to a function.

我正在尝试创建一个接受一个字符的函数,然后根据字符是什么返回一个指向函数的指针。我只是不确定如何使函数返回指向函数的指针。

回答by erikkallen

int f(char) {
    return 0;
}

int (*return_f())(char) {
    return f;
}

No, seriously, use a typedef :)

不,说真的,使用 typedef :)

回答by erikkallen

#include <iostream>
using namespace std;

int f1() {
    return 1;
}

int f2() {
    return 2;
}

typedef int (*fptr)();


fptr f( char c ) {
    if ( c == '1' ) {
        return f1;
    }
    else {
        return f2;
    }
}

int main() {
    char c = '1';
    fptr fp = f( c );
    cout << fp() << endl;
}

回答by sean e

Create a typedef for the function signature:

为函数签名创建一个 typedef:

typedef void (* FuncSig)(int param);

Then declare your function as returning FuncSig:

然后将您的函数声明为返回 FuncSig:

FuncSig GetFunction();

回答by VINOTH ENERGETIC

Syntax for returning the function:

返回函数的语法:

return_type_of_returning_function (*function_name_which_returns_function)(actual_function_parameters) (returning_function_parameters)

Eg: Consider the function that need to be returned as follows,

例如:考虑如下需要返回的函数,

void* (iNeedToBeReturend)(double iNeedToBeReturend_par)
{
}

Now the iNeedToBeReturend function can be returned as

现在 iNeedToBeReturend 函数可以返回为

void* (*iAmGoingToReturn(int iAmGoingToReturn_par))(double)
{
   return iNeedToBeReturend;
}

I Felt very bad to learn this concept after 3 years of professional programming life.

经过 3 年的专业编程生涯后,我对学习这个概念感到非常难过。

Bonus for you waiting down for dereferencing function pointer.

等待取消引用函数指针的奖励。

C++ Interview questions

C++面试题

Example for function which returns the function pointer is dlopen in dynamic library in c++

返回函数指针的函数示例为 c++ 动态库中的 dlopen

回答by Superfly Jon

In C++11 you can use trailing return types to simplify the syntax, e.g. assuming a function:

在 C++11 中,您可以使用尾随返回类型来简化语法,例如假设一个函数:

int c(int d) { return d * 2; }

This can be returned from a function (that takes a double to show that):

这可以从一个函数返回(需要一个双精度来表示):

int (*foo(double e))(int)
{
    e;
    return c;
}

Using a trailing return type, this becomes a bit easier to read:

使用尾随返回类型,这变得更容易阅读:

auto foo2(double e) -> int(*)(int)
{
    e;
    return c;
}

回答by ralphtheninja

typedef void (*voidFn)();

void foo()
{
}

voidFn goo(char c)
{
    if (c == 'f') {
        return foo;
    }
    else {
        //..
    }
    // ..
}

回答by user2875784

Here is how to do it without using a typedef:

以下是不使用 typedef 的方法:

int c(){ return 0; }

int (* foo (void))(){  //compiles
return c;
}

回答by m-sharp

Check out this site - http://cdecl.org

看看这个网站 - http://cdecl.org

Helps you convert english to C declarations and back!

帮助您将英语转换为 C 声明并返回!

Cool Stuff!

很酷的东西!

This link decodes the example in erikallen's answer. int (*return_f())(char)

此链接对 erikallen 的答案中的示例进行了解码。 int (*return_f())(char)

回答by Thomas Jung

This is the code to show return of a function pointer. You need to define the "function signature" to return first:

这是显示函数指针返回的代码。您需要先定义要返回的“函数签名”:

int returnsOne() {
     return 1;
}

typedef int(*fp)();

fp returnsFPtoReturnsOne() {
    &returnsOne;
}

In your specific case:

在您的具体情况下:

fp getFunctionFor(char code) {
    switch (code) {
        case 'a': return &functionA;
        case 'b': return &functionB;
    }
    return NULL;
}

回答by Chris Dodd

Easiest way is to typedef the pointer-to-function type you want, and then use that

最简单的方法是 typedef 你想要的函数指针类型,然后使用它

typedef void (*fnptr_t)(int, int);
fptr_t myfunc(char *) { ....