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.
我正在尝试创建一个函数,它取一个字符,然后返回一个指向一个函数的指针,这取决于字符是什么。我只是不知道如何让一个函数返回一个指向一个函数的指针。
13 个解决方案
#1
84
int f(char) {
return 0;
}
int (*return_f())(char) {
return f;
}
No, seriously, use a typedef :)
不,认真地说,使用typedef:)
#2
62
#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;
}
#3
16
Create a typedef for the function signature:
为函数签名创建一个类型定义:
typedef void (* FuncSig)(int param);
Then declare your function as returning FuncSig:
然后将函数声明为返回的函数:
FuncSig GetFunction();
#4
4
typedef void (*voidFn)();
void foo()
{
}
voidFn goo(char c)
{
if (c == 'f') {
return foo;
}
else {
//..
}
// ..
}
#5
3
Here is how to do it without using a typedef:
下面是如何在不使用typedef的情况下做到这一点:
int c(){ return 0; }
int (* foo (void))(){ //compiles
return c;
}
#6
2
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())(字符)
#7
2
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
现在,失去亲人的功能可以返回as
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++的面试问题
Example for function which returns the function pointer is dlopen in dynamic library in c++
函数返回函数指针的例子在c++的动态库中是dlopen
#8
1
Easiest way is to typedef the pointer-to-function type you want, and then use that
最简单的方法是定义您想要的pointerto -function类型,然后使用它。
typedef void (*fnptr_t)(int, int);
fptr_t myfunc(char *) { ....
#9
1
I prefer returning objects and call the operator(). This way your function can return an interface and all classes can inherit from this. That is, if you're using C++ and not C.
我更喜欢返回对象并调用操作符()。这样,函数就可以返回一个接口,所有的类都可以继承这个接口。也就是说,如果你用的是c++而不是C。
Then you can use the parametrized factor method to return the objects based on your input.
然后可以使用参数化因子方法根据输入返回对象。
#10
1
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;
}
#11
1
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;
}
#12
0
Something like this
像这样的东西
#include <iostream>
typedef char (*fn_ptr_t)(char);
char a_fn(char c)
{
return c + 1;
}
char b_fn(char c)
{
return c + 2;
}
fn_ptr_t
return_function(char c)
{
fn_ptr_t result = 0;
switch (c)
{
case 'a':
result = a_fn;
break;
case 'b':
result = b_fn;
break;
}
return result;
}
int
main()
{
fn_ptr_t fn = return_function('a');
std::cout << "a(l) = " << (fn)('l') << std::endl;
return 0;
}
#13
-1
I'm assuming C here (no objects) :) :
我假设这里是C(没有对象):
// Type of function which takes a char and returns an int:
typedef int (*Func)(char a);
// An example of the function you're trying to return and which does something
// with char:
int exampleFunc(char a)
{
return (int)(a + 42);
}
// The function returning the pointer to a function:
Func *returnAfunc(void)
{
return exampleFunc;
}
#1
84
int f(char) {
return 0;
}
int (*return_f())(char) {
return f;
}
No, seriously, use a typedef :)
不,认真地说,使用typedef:)
#2
62
#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;
}
#3
16
Create a typedef for the function signature:
为函数签名创建一个类型定义:
typedef void (* FuncSig)(int param);
Then declare your function as returning FuncSig:
然后将函数声明为返回的函数:
FuncSig GetFunction();
#4
4
typedef void (*voidFn)();
void foo()
{
}
voidFn goo(char c)
{
if (c == 'f') {
return foo;
}
else {
//..
}
// ..
}
#5
3
Here is how to do it without using a typedef:
下面是如何在不使用typedef的情况下做到这一点:
int c(){ return 0; }
int (* foo (void))(){ //compiles
return c;
}
#6
2
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())(字符)
#7
2
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
现在,失去亲人的功能可以返回as
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++的面试问题
Example for function which returns the function pointer is dlopen in dynamic library in c++
函数返回函数指针的例子在c++的动态库中是dlopen
#8
1
Easiest way is to typedef the pointer-to-function type you want, and then use that
最简单的方法是定义您想要的pointerto -function类型,然后使用它。
typedef void (*fnptr_t)(int, int);
fptr_t myfunc(char *) { ....
#9
1
I prefer returning objects and call the operator(). This way your function can return an interface and all classes can inherit from this. That is, if you're using C++ and not C.
我更喜欢返回对象并调用操作符()。这样,函数就可以返回一个接口,所有的类都可以继承这个接口。也就是说,如果你用的是c++而不是C。
Then you can use the parametrized factor method to return the objects based on your input.
然后可以使用参数化因子方法根据输入返回对象。
#10
1
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;
}
#11
1
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;
}
#12
0
Something like this
像这样的东西
#include <iostream>
typedef char (*fn_ptr_t)(char);
char a_fn(char c)
{
return c + 1;
}
char b_fn(char c)
{
return c + 2;
}
fn_ptr_t
return_function(char c)
{
fn_ptr_t result = 0;
switch (c)
{
case 'a':
result = a_fn;
break;
case 'b':
result = b_fn;
break;
}
return result;
}
int
main()
{
fn_ptr_t fn = return_function('a');
std::cout << "a(l) = " << (fn)('l') << std::endl;
return 0;
}
#13
-1
I'm assuming C here (no objects) :) :
我假设这里是C(没有对象):
// Type of function which takes a char and returns an int:
typedef int (*Func)(char a);
// An example of the function you're trying to return and which does something
// with char:
int exampleFunc(char a)
{
return (int)(a + 42);
}
// The function returning the pointer to a function:
Func *returnAfunc(void)
{
return exampleFunc;
}