返回的函数指针类型 [英] Returning function pointer type

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

问题描述

我常常觉得有必要写这回函数指针的函数。每当我做的,我用的基本格式是:

 的typedef INT(*函式)(INT,INT);函数_ getFunc()
{
   函式测试;
   测试/ * = ... * /;
   回归测试;
}

然而,这可以与大量的功能处理,所以我想不必声明的typedef为每一个时很麻烦(或每类功能)

我可以删除的typedef并声明在函数返回局部变量:
INT(*测试)(int类型的,INT B);函数体外观制作是这样的:

  {
     INT(*测试)(int类型的,INT B);
     测试/ * = ... * /;
     回归测试;
}

但我不知道是什么,为函数的返回类型设置。
我曾尝试:

  INT(*)(INT,INT)getFunc()
{
    INT(*测试)(int类型的,INT B);
    测试/ * = ... * /;
    回归测试;
}

但报告语法错误。我如何申报这样的函数的返回类型没有声明的typedef的函数指针。它甚至有可能?另外请注意,据我所知,好像这将是更清洁的申报类型定义,每一个功能,但是,我很小心,我的结构code是干净,易于遵循越好。我想消除的typedef的原因是,他们往往只用来声明检索功能,因此似乎在code多余的。


解决方案

  INT(* getFunc())(INT,INT){...}

Often I find the need to write functions which return function pointers. Whenever I do, the basic format I use is:

typedef int (*function_type)(int,int);

function_type getFunc()
{
   function_type test;
   test /* = ...*/;
   return test;
}

However this can get cumbersome when dealing with a large number of functions so I would like to not have to declare a typedef for each one (or for each class of functions)

I can remove the typedef and declare the local variable returned in the function as: int (*test)(int a, int b); making the function body look like this:

{
     int (*test)(int a, int b);
     test /* = ...*/;
     return test;
}

but then I do not know what to set for the return type of the function. I have tried:

int(*)(int,int) getFunc()
{
    int (*test)(int a, int b);
    test /* = ...*/;
    return test;
}

but that reports a syntax error. How do I declare the return type for such a function without declaring a typedef for the function pointer. Is it even possible? Also note that I am aware that it seems like it would be cleaner to declare typedefs, for each of the functions, however, I am very careful to structure my code to be as clean and easy to follow as possible. The reason I would like to eliminate the typedefs is that they are often only used to declare the retrieval functions and therefore seem redundant in the code.

解决方案

int (*getFunc())(int, int) { … }

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

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