函数返回自身 [英] Function Returning Itself
问题描述
是否可以声明一些函数类型 func_t
返回该类型,func_t
?
Is it possible to declare some function type func_t
which returns that type, func_t
?
换句话说,函数是否有可能返回自身?
In other words, is it possible for a function to return itself?
// func_t is declared as some sort of function pointer
func_t foo(void *arg)
{
return &foo;
}
或者我必须使用 void *
和类型转换?
Or would I have to use void *
and typecasting?
推荐答案
不,您不能在 C 中声明递归函数类型.除了在结构(或联合)内部,不可能在 C 中声明递归类型.
No, you cannot declare recursive function types in C. Except inside a structure (or an union), it's not possible to declare a recursive type in C.
现在对于 void *
解决方案,void *
只能保证保存指向对象的指针,而不是指向函数的指针.能够转换函数指针和 void *
仅作为扩展可用.
Now for the void *
solution, void *
is only guaranteed to hold pointers to objects and not pointers to functions. Being able to convert function pointers and void *
is available only as an extension.
这篇关于函数返回自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!