返回一个函数指针 [英] Returning a function pointer

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

问题描述

定义一个返回指向

函数的函数的函数是什么?具体来说,我想要一个带有int的函数,并且

返回一个指向函数的指针,该函数接受一个int并返回一个字符串。


我试过这个:


gchar *(* f(gint n))(gint)

{

/ *逻辑这里* /

}


但这似乎不起作用。


:(

解决方案

Antoninus Twink写道:


什么是正确的语法定义一个返回指向

a函数的函数?具体来说,我想要一个带有int的函数,并且

返回一个指向函数的指针int并返回一个字符串。


我试过这个:


gchar *(* f(gint n))(gint)

{

/ *逻辑在这里* /

}


但这似乎不起作用。


:(



我从来没有设法做到这一点out a typedef ...


char * fn1(int a)

{

return" function 1"; < br $>
}


char * fn2(int a)

{

return" function 2" ;

}


typedef char *(* FunctionType)(int);

FunctionType FunctionReturningAFunctionPointer(int a)

{

if(a 0)

return fn1;

else

return fn2;

}


Antoninus Twink说:


什么是正确的语法定义一个函数返回一个指针

到函数?具体来说,我想要一个带有int,

的函数,并返回一个指向函数的指针,该函数接受一个int并返回一个

字符串。



char * foo(int x)

{

static char bar [2];

bar [0] = x;

返回栏;

}


char *(* baz( int n))(int)

{

/ *以某种方式使用n,我想* /


返回foo ;

}


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


Antoninus Twink写道:


>

定义一个返回

指向函数的函数的正确语法是什么?具体来说,我想要一个函数,

接受一个int,并返回一个指向一个函数的指针,该函数接受一个

int并返回一个字符串。


我试过这个:


gchar *(* f(gint n))(gint)

{

/ *逻辑在这里* /

}


但这似乎不起作用。


:(



怎么样


typedef char * stringfromint(int) ;

stringfromint * transfer(int){

...惊人的代码...

};


-

Vista终于免于入侵。没有人会''破解''

产品激活并试图窃取o / s。任何聪明的人足够这么做也很聪明,不想打扰。

-

发表于来自 http://www.teranews.com 的免费Usenet帐户


What''s the correct syntax to define a function that returns a pointer to
a function? Specifically, I''d like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn''t seem to work.

:(

解决方案

Antoninus Twink wrote:

What''s the correct syntax to define a function that returns a pointer to
a function? Specifically, I''d like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn''t seem to work.

:(

I have never managed to do it without a typedef...

char *fn1(int a)
{
return "function 1";
}

char *fn2(int a)
{
return "function 2";
}

typedef char *(*FunctionType)(int);
FunctionType FunctionReturningAFunctionPointer(int a)
{
if (a 0)
return fn1;
else
return fn2;
}


Antoninus Twink said:

What''s the correct syntax to define a function that returns a pointer
to a function? Specifically, I''d like a function that takes an int,
and returns a pointer to a function that takes an int and returns a
string.

char *foo(int x)
{
static char bar[2];
bar[0] = x;
return bar;
}

char *(*baz(int n))(int)
{
/* use n in some way, I guess */

return foo;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Antoninus Twink wrote:

>
What''s the correct syntax to define a function that returns a
pointer to a function? Specifically, I''d like a function that
takes an int, and returns a pointer to a function that takes an
int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn''t seem to work.

:(

How about:

typedef char *stringfromint(int);
stringfromint *transfer(int) {
... amazing code ...
};

--
"Vista is finally secure from hacking. No one is going to ''hack''
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."
--
Posted via a free Usenet account from http://www.teranews.com


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

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