函数返回函数指针? [英] Function returning a function pointer?

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

问题描述

你如何写一个返回函数指针的函数?为什么你需要这样做?
?是吧:


int(*)(int&)fn(int& arg);


谢谢!!!

解决方案

Protoman写道:

你如何写一个函数返回一个函数指针,为什么
你会需要这样做吗?


一个函数返回函数指针有很多原因。
最明显的是:


- 为用户提供访问更多信息的渠道

- 提供可以链调用的跟进

是:

int( *)(int&)fn(int& arg);

谢谢!!!




正确的声明是:


int(* fn(int& arg))(int&);


如果这看起来很神秘,请在外面阅读:


-返回类型是指向int(int&)的指针因此:


int(* [...])(int&);


- [...]函数fn本身因此:


int(*

fn(int& ; arg)

)(int& arg);


或者,您可以提前输入退款类型:


typedef int(* fn_ptr)(int&);

fn_ptr fn(int& arg);


问候,

Ben


" Protoman" <镨********** @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com

你会如何写一个返回函数指针的函数?为什么你需要这样做?是吗:

int(*)(int&)fn(int& arg);

谢谢!!!




给定函数


int foo(int& x)

{

++ x;

返回x;

}


a函数,它接受一个int参数参数arg并返回一个

指向foo的指针采用的形式


int(* fn(int& arg))(int&)

{

返回& foo; //&是可选的

}


但是,如果你这样做,你会减少你的大脑压力:


typedef int(* fnptr)(int&);


fnptr fn(int& arg)

{

return& amp; ; foo;

}


至于为什么你需要这样做,也许fn会选择

相应的函数指针该程序需要使用,基于

计算它与arg。


-

John Carson


我可以使用这样的构造进行currying吗?


How would you write a function returning a function pointer and why
would you need to do this? Is it:

int(*)(int&) fn(int& arg);

Thanks!!!

解决方案

Protoman wrote:

How would you write a function returning a function pointer and why
would you need to do this?
There are many reason why one function would return a function pointer.
The most obvious are:

- to give user a channel to access futher information
- to give follow up which can be chain-invoked
Is it:

int(*)(int&) fn(int& arg);

Thanks!!!



The correct declaration is:

int (*fn(int& arg))(int&);

If this seems cryptic, read it outside in:

-the return type is a pointer to int(int&) therefore:

int (* [...])(int&);

-the [...] is the function fn itself therefore:

int (*
fn(int& arg)
)(int& arg);

Alternatively, you can typedef the return type in advance:

typedef int(*fn_ptr)(int&);
fn_ptr fn(int& arg);

Regards,
Ben


"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com

How would you write a function returning a function pointer and why
would you need to do this? Is it:

int(*)(int&) fn(int& arg);

Thanks!!!



Given a function

int foo(int &x)
{
++x;
return x;
}

a function that takes an int reference parameter arg and will return a
pointer to foo takes the form

int (*fn(int& arg))(int &)
{
return &foo; // the & is optional
}

However, you will give yourself less brain strain if you do it this way:

typedef int (*fnptr)(int&);

fnptr fn(int& arg)
{
return &foo;
}

As for why you would need to do this, perhaps fn would select the
appropriate function pointer the program needs to use, based on the
calculations it does with arg.

--
John Carson


Can I use such a construct to do currying?


这篇关于函数返回函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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